一个 WCF 中的多个合同 service_But 调用第二个合同时出错
Multiple contracts in one WCF service_But error for calling the second contract
我在一个 WCF 中有多个合同:
namespace SysLap.Services.Web.DataExtractor
{
[ServiceContract]
public interface IServiceDataExtractor
{
[OperationContract]
[WebGet]
List<User> GetAllUsers(/*CommonParams commonParams*/);
}
[ServiceContract]
public interface IScopesExtract
{
[OperationContract]
[WebGet]
List<SyScope> GetAllScopes();
}
}
并且 Web.config 配置良好:
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IServiceDataExtractor" />
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IScopesExtract" />
</service>
对于服务GetAllUsers,它运行良好。但是对于 GetAllScopes 服务,当我使用 SOAP UI 对其进行测试时,出现此错误:
<p class="heading1">Request Error</p>
<p>The server encountered an error processing the request. The exception message is 'Multiple filters matched.'.
See server logs for more details. The exception stack trace is: </p>
<p> at System.ServiceModel.Dispatcher.EndpointDispatcherTable.LookupInCache(Message message, Boolean&
addressMatched)
at System.ServiceModel.Dispatcher.EndpointDispatcherTable.Lookup(Message message, Boolean&
addressMatched)
at System.ServiceModel.Dispatcher.ChannelHandler.GetDatagramChannel(Message message, EndpointDispatcher&
endpoint, Boolean& addressMatched)
at System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request)
at System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContextCore(RequestContext request)
</p>
我该如何解决?
谢谢
您基本上只需要两个单独的端点 address
。
更改配置并提供唯一地址,例如:
<endpoint address="DataExtractor" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IServiceDataExtractor" />
<endpoint address="ScopesExtractor" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IScopesExtract" />
当我们使用WCF项目模板创建WCF服务时,通常Service.svc(Markup)中只有一个服务宿主。
<%@ ServiceHost Language="C#" Debug="true" Service="WcfService3.Service1" CodeBehind="Service1.svc.cs" %>
这意味着我们可以有多个服务合同,但我们只能有一个服务(服务实现class)。
在这种情况下,在一个项目中托管多个服务合同是有效的。
public class Service1 : IService1,IService2
回到你的问题,错误主要是我们的服务有相同的相对地址,这不符合WCF寻址规范。
正如所说,我们需要两个独立的端点地址。
<services>
<service name="WcfService3.Service1">
<endpoint address="a" binding="webHttpBinding" contract="WcfService3.IService1" behaviorConfiguration="rest"></endpoint>
<endpoint address="b" binding="webHttpBinding" contract="WcfService3.IService2" behaviorConfiguration="rest"></endpoint>
</service>
</services>
服务 Url 将是
http://xxxx:xx/service1.svc/a/GetAllUsers
http://xxxx:xx/service1.svc/b/GetAllScopes
如果有什么我可以帮忙的,请随时告诉我。
我在一个 WCF 中有多个合同:
namespace SysLap.Services.Web.DataExtractor
{
[ServiceContract]
public interface IServiceDataExtractor
{
[OperationContract]
[WebGet]
List<User> GetAllUsers(/*CommonParams commonParams*/);
}
[ServiceContract]
public interface IScopesExtract
{
[OperationContract]
[WebGet]
List<SyScope> GetAllScopes();
}
}
并且 Web.config 配置良好:
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IServiceDataExtractor" />
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IScopesExtract" />
</service>
对于服务GetAllUsers,它运行良好。但是对于 GetAllScopes 服务,当我使用 SOAP UI 对其进行测试时,出现此错误:
<p class="heading1">Request Error</p>
<p>The server encountered an error processing the request. The exception message is 'Multiple filters matched.'.
See server logs for more details. The exception stack trace is: </p>
<p> at System.ServiceModel.Dispatcher.EndpointDispatcherTable.LookupInCache(Message message, Boolean&
addressMatched)
at System.ServiceModel.Dispatcher.EndpointDispatcherTable.Lookup(Message message, Boolean&
addressMatched)
at System.ServiceModel.Dispatcher.ChannelHandler.GetDatagramChannel(Message message, EndpointDispatcher&
endpoint, Boolean& addressMatched)
at System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request)
at System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContextCore(RequestContext request)
</p>
我该如何解决?
谢谢
您基本上只需要两个单独的端点 address
。
更改配置并提供唯一地址,例如:
<endpoint address="DataExtractor" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IServiceDataExtractor" />
<endpoint address="ScopesExtractor" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="SysLap.Services.Web.DataExtractor.IScopesExtract" />
当我们使用WCF项目模板创建WCF服务时,通常Service.svc(Markup)中只有一个服务宿主。
<%@ ServiceHost Language="C#" Debug="true" Service="WcfService3.Service1" CodeBehind="Service1.svc.cs" %>
这意味着我们可以有多个服务合同,但我们只能有一个服务(服务实现class)。
在这种情况下,在一个项目中托管多个服务合同是有效的。
public class Service1 : IService1,IService2
回到你的问题,错误主要是我们的服务有相同的相对地址,这不符合WCF寻址规范。
正如
<services>
<service name="WcfService3.Service1">
<endpoint address="a" binding="webHttpBinding" contract="WcfService3.IService1" behaviorConfiguration="rest"></endpoint>
<endpoint address="b" binding="webHttpBinding" contract="WcfService3.IService2" behaviorConfiguration="rest"></endpoint>
</service>
</services>
服务 Url 将是
http://xxxx:xx/service1.svc/a/GetAllUsers
http://xxxx:xx/service1.svc/b/GetAllScopes
如果有什么我可以帮忙的,请随时告诉我。