因此,出于某些组织方面的考虑,我将最初的单一接口(interface)契约拆分为两个独立的契约。现在,我只有一个实现这两个接口(interface)的类,我想通过 WCF 将它们全部用作 REST 服务。 契约(Contract)如下:
第一:
[ServiceContract]
public interface INotification
{
[OperationContract]
[WebGet]
XElement GetInfo(string appID);
[OperationContract]
[WebGet]
void RegisterRID(string appID, string registrationID);
}
第二个:
[ServiceContract]
public interface IPlanning
{
[OperationContract]
[WebGet]
XElement PlanTrip(string toDestination, string fromDestination, int year, int month, int day, int hour, int minute, string appID);
[OperationContract]
[WebGet]
XElement PlanTripDelayed(string toDestination, string fromDestination, int year, int month, int day, int hour, int minute, string appID);
[OperationContract]
[WebGet]
XElement PlanTripLoc(string toDestination, string fromLat, string fromLong, int year, int month, int day, int hour, int minute, string appID);
}
我的 app.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="TravelPlannerWebService.Domain.Entity.ETravelPlanner">
<endpoint binding="webHttpBinding" bindingConfiguration="" contract="TravelPlannerWebService.Acquaintance.IPlanning" behaviorConfiguration="RESTFriendly" />
<endpoint binding="webHttpBinding" bindingConfiguration="" contract="TravelPlannerWebService.Acquaintance.INotification" behaviorConfiguration="RESTFriendly" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:80" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
问题是,每当我尝试使用两个接口(interface)时,都会出现错误。通过查看我的 app_tracelog.svclog(我在这里故意从 app.config 中删除了调试代码),我可以看到我收到以下错误:
消息:多个过滤器匹配。
堆栈跟踪:
System.ServiceModel.Dispatcher.EndpointDispatcherTable.LookupInCache(Message message, Boolean& addressMatched)
System.ServiceModel.Dispatcher.EndpointDispatcherTable.Lookup(Message message, Boolean& addressMatched)
System.ServiceModel.Dispatcher.ChannelHandler.GetDatagramChannel(Message message, EndpointDispatcher& endpoint, Boolean& addressMatched)
System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request)
System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContext(RequestContext request)
System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
System.Runtime.InputQueue`1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)
System.Runtime.InputQueue`1.EnqueueAndDispatch(T item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult result)
System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult result)
System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
System.Net.LazyAsyncResult.Complete(IntPtr userToken)
System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
我不知道如何解决这个问题。据我所知,根据以下指南,这是对多个端点使用相同类型的正确方法。有什么想法吗?
最佳答案
你可以使用下面的代码看看你是否仍然得到异常:
<service name="TravelPlannerWebService.Domain.Entity.ETravelPlanner">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="" contract="TravelPlannerWebService.Acquaintance.IPlanning" behaviorConfiguration="RESTFriendly" />
<endpoint address="more" binding="webHttpBinding" bindingConfiguration="" contract="TravelPlannerWebService.Acquaintance.INotification" behaviorConfiguration="RESTFriendly" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:80" />
</baseAddresses>
</host>
</service>
您不能让 2 个不同的端点监听同一地址。因此,我添加了 address 属性来区分它们。
在 IIS 中托管时,您的 URL 可能如下所示:
http://localhost/ApplicationVD/PlanTrip
http://localhost/ApplicationVD/PlanTripDelayed
http://localhost/ApplicationVD/PlanTripLoc
http://localhost/ApplicationVD/More/GetInfo
http://localhost/ApplicationVD/More/RegisterRID
关于c# - "Multiple filters matched."在 WCF 中对多个终结点使用相同的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8476748/