2016-11-14 45 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,很高兴帮助!我很感谢您为添加足够的信息来清晰地表达您的问题所做的努力 –