我是WCF的初学者,并且创建了一个名为OrderProcessor的RESTful服务,它具有以下三个操作:
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。例如,我尝试检查URL:
http://localhost:4723/OrderProcessor/api
它抛出以下错误:
在合同“ IOrderProcessor”中,存在多个操作
方法“ POST”和一个等效于
'/ api / {orderNumber}'。每个操作都需要以下各项的唯一组合:
UriTemplate和Method可以明确分配消息。使用
WebGetAttribute或WebInvokeAttribute来更改UriTemplate和
操作的方法值。
我试图搜索此错误,有人建议放
实现类上的“ [[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]]”,但错误仍在此处[AddressFilter mismatch at the EndpointDispatcher - the msg with To。有人可以像WebAPI一样建议一种使用URL的方法吗?
最佳答案
您下面的两种服务方法的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}"