2011-12-12 45 views
3

因此,由於一些組織擔心,我已經分手了什麼intially單一界面合同分爲兩個獨立的。現在,我只有一個類實現兩個接口,我想讓他們都可以作爲REST服務通過WCF。 的合同如下:「多個過濾器相匹配。」在WCF當使用相同的地址多個端點

1:

[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> 

問題在於,無論何時嘗試使用兩個接口,都會出現錯誤。通過看我的app_tracelog.svclog(我已經刪除調試代碼的app.config故意在這裏),我可以看到,我得到以下錯誤:

消息:多重過濾器進行匹配。

堆棧跟蹤:

System.ServiceModel.Dispatcher.EndpointDispatcherTable.LookupInCache(Message message, Boolean&amp; addressMatched) 
System.ServiceModel.Dispatcher.EndpointDispatcherTable.Lookup(Message message, Boolean&amp; addressMatched) 
System.ServiceModel.Dispatcher.ChannelHandler.GetDatagramChannel(Message message, EndpointDispatcher&amp; endpoint, Boolean&amp; 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) 

我不知道如何解決這個問題。據我所看到的,從以下指南,這是用於多個端點同一類型的正確方法。有任何想法嗎?

回答

0

你可以使用下面,看看你仍然可以得到一個異常:

<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個不同的端點監聽同一地址。因此,我添加了地址屬性來區分它們。

你的URL可以是託管在IIS的時候如下:

http://localhost/ApplicationVD/PlanTrip 
http://localhost/ApplicationVD/PlanTripDelayed 
http://localhost/ApplicationVD/PlanTripLoc 
http://localhost/ApplicationVD/More/GetInfo 
http://localhost/ApplicationVD/More/RegisterRID 
+0

奇怪,因爲在http://msdn.microsoft.com/en-us/library/aa395210.aspx的例子,他們使用相同的不會忽略的兩個端點與單獨的合同? –

+0

如果你看看他們用的是ListenUri屬性,它的值設置的文章,該屬性可以讓你有不同的合同監聽同一地址。 – Rajesh

+0

好,只是認爲這是沒有必要,因爲ListenUri有兩個相同的值。將嘗試這種方法。 –

0

這裏簡短的回答是,你需要記住的 靛藍 的ABC ......啊哈... WCF:地址,綁定,合同。

您的服務端點在同一個物理類中實現的事實與您服務的調用者無關:對於外部世界,您的服務現在通過兩個不同的服務端點公開:IPlanning & INotification。

修改你的app.config文件的端點暴露在它自己的地址,每個服務端點(相對於你的服務的基址):

<endpoint name ="TripPlanningEndpoint" 
    address="Planning" binding="webHttpBinding" contract="Service.IPlanning" 
    behaviorConfiguration="RESTFriendly" /> 

<endpoint name="TripNotificationsEndpoint" 
    address="Notifications" binding="webHttpBinding" contract="Service.INotification" 
    behaviorConfiguration="RESTFriendly" /> 

哦......你並不需要所有的REST友好端點行爲的東西 - 的WebHttpBinding已經支持HTTPGET默認。 :)

另外,我強烈建議您啓用服務元數據(ESP」,而發展中國家),因爲這可以讓你使用工具來生成客戶端代理類,允許您快速建立(正確配置)客戶端應用程序和試驗。

到您的服務的應用程序。配置類,添加以下內容:

<system.serviceModel> 
    <behaviors> 
    ... 
     <serviceBehaviors> 
      <behavior name="ServiceMetadataBehavior"> 
       <serviceMetadata httpGetEnabled="true"/> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 

也會增加一個額外的端點暴露你的服務的元數據:

<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 

現在,您可以添加對使用「添加服務引用」客戶端/測試應用程序: Multi-interface WCF Service - Add Reference Dialog

HTH。

+0

在http://msdn.microsoft.com/en-us/library/aa395210.aspx,他們實際上表明它可能有兩個端點,與單獨的合同,共享相同的地址?我錯了嗎? 我故意刪除服務元數據,因爲所有方法的測試在前一個實現中只用一個接口就已經完成了。重構是由於架構問題(提供了更好的概述)而完成的。 –

相關問題