gpt4 book ai didi

c# - WCF 中用于操作契约(Contract)的路由

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:56 25 4
gpt4 key购买 nike

我是 WCF 的初学者,我创建了一个名为 OrderProcessor 的 RESTful 服务,其中包含三个操作:

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

我需要就与同一服务相关的几点提出建议和反馈:1. 属性路由:我知道像在 WebAPI 中一样,属性路由在 WCF 中是不可能的,但我想使用以下 URL 创建 api:
<a href="http://localhost" rel="noreferrer noopener nofollow">http://localhost</a>:{portnumber}/OrderProcessor/IsClientActive/{token} - <strong>POST</strong> request for <strong>IsClientActive() method</strong>
<a href="http://localhost" rel="noreferrer noopener nofollow">http://localhost</a>:{portnumber}/OrderProcessor/ProcessOrder - <strong>GET</strong> request for the <strong>ProcessOrder() method</strong>
<a href="http://localhost" rel="noreferrer noopener nofollow">http://localhost</a>:{portnumber}/OrderProcessor/CheckStatus/{orderNumber} - <strong>POST</strong> request for the <strong>CheckStatus() method</strong>

所以,我定义了服务的接口(interface)和实现如下:
契约(Contract) - 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它抛出以下错误:

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 .有人可以建议一种使用 URL 的方式,就像 WebAPI 方式一样吗?

最佳答案

对于您的以下两种服务方法,只需使用 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}"

关于c# - WCF 中用于操作契约(Contract)的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com