2016-11-14 53 views
2

我在WCF一個初學者,創建了一個稱爲訂單處理器RESTful服務有三種操作:路由在WCF的業務合同

bool IsClientActive(string token); 
Order ProcessOrder(); 
string CheckStatus(Guid orderNumber); 

我需要在與同一服務幾點建議和反饋: 1.屬性路由:我知道像的WebAPI,屬性路由是不是在WCF可能的,但我想創建具有以下URL的API: http://localhost :{portnumber}/OrderProcessor/IsClientActive/{token} - POST request for IsClientActive() method http://localhost :{portnumber}/OrderProcessor/ProcessOrder - GET request for the ProcessOrder() method http://localhost :{portnumber}/OrderProcessor/CheckStatus/{orderNumber} - POST request for the CheckStatus() method

所以,我定義爲服務的接口和實現如下:
個 合同 - IOrderProcessor.cs

interface IOrderProcessor 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,  ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{token}")] 
    bool IsClientActive(string token); 

    [OperationContract(IsOneWay = false)] 
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api")] 
    Order ProcessOrder(); 

    [OperationContract] 
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{orderNumber}")] 
    string CheckStatus(Guid orderNumber); 
} 

實現 - OrderProcessor.cs

public class OrderProcessor : IOrderProcessor 
    { 
     public bool IsClientActive(string token) 
     { 
      bool status = false; 
      try 
      { 
       if (!string.IsNullOrEmpty(token.Trim())) 
       { 
        //Do db checking 
        status = true; 
       } 
       status = false; 
      } 
      catch (Exception ex) 
      { 
       //Log exception 
       throw ex; 
      } 
      return status; 
     } 

     public Order ProcessOrder() 
     { 
      Order newOrder = new Order() 
      { 
       Id = Guid.NewGuid(), 
       Owner = "Admin", 
       Recipient = "User", 
       Info = "Information about the order", 
       CreatedOn = DateTime.Now 
      }; 
      return newOrder; 
     } 

     public string CheckStatus(Guid orderNumber) 
     { 
      var status = string.Empty; 
      try 
      { 
       if (!(orderNumber == Guid.Empty)) 
       { 
        status = "On Track"; 
       } 
       status = "Order Number is invalid"; 

      } 
      catch (Exception) 
      { 
       //Do logging 
       throw; 
      } 

      return status; 
     } 
    } 

的Web.config

<system.serviceModel> 
    <services> 
     <service name="WCF_MSMQ_Service.OrderProcessor" behaviorConfiguration="ServiceBehavior"> 
     <!-- Service Endpoints --> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:4723/"/> 
      </baseAddresses> 
     </host> 

     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address="" binding="webHttpBinding" contract="WCF_MSMQ_Service.IOrderProcessor" behaviorConfiguration="Web"></endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- Enable metadata publishing. --> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 

      <!-- To receive exception details in faults for debugging purposes, set the value below to true. 
      Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 

     <endpointBehaviors> 
     <behavior name="Web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

問題: 我已經實現了所有的代碼,但是當我嘗試使用Visual Studio運行它(在瀏覽器中查看),我無法訪問上面定義的URL。例如,我想查詢的網址: http://localhost:4723/OrderProcessor/api 它拋出以下錯誤:

In contract 'IOrderProcessor', there are multiple operations with Method 'POST' and a UriTemplate that is equivalent to '/api/{orderNumber}'. Each operation requires a unique combination of UriTemplate and Method to unambiguously dispatch messages. Use WebGetAttribute or WebInvokeAttribute to alter the UriTemplate and Method values of an operation.

我試圖尋找這個錯誤,有人建議把 「[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any) ]「在實施,智力班,但錯誤仍然在這裏[AddressFilter mismatch at the EndpointDispatcher - the msg with To。有人可以建議一種像WebAPI一樣使用URL的方法嗎?

+0

正如@Mukesh Modhvadiya所建議的那樣,我一直在爲IsClientActive()保留相同的UriTemplate,和CheckStatus()方法。解決方案是指定不同的名稱,它的工作。 –

回答

1

簡單UriTemplate爲您的以下服務方法沒有區別,

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,  ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{token}")] 
bool IsClientActive(string token); 

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{orderNumber}")] 
string CheckStatus(Guid orderNumber); 

爲了區別,你可以通過UriTemplate

UriTemplate = "/api/isClientActive/{token}" 

UriTemplate = "/api/checkStatus/{orderNumber}" 
添加方法名如下更改
+0

感謝您指出UriTemplate的相似性,我將它修復並立即開始工作。 –

+0

@iSahilSharma,很高興幫助!我很感謝您爲添加足夠的信息來清晰地表達您的問題所做的努力 –