2016-11-10 25 views
2

的定製服務主機廠我創建的VisualStudio使用2015年我執行一個新的WCFServiceLibrary,一切都很好,我可以看到WCF測試客戶端與樣品的方法,我可以從瀏覽器訪問。調試在WCF

現在我想添加一個自定義服務主機工廠。我加入這個類:

namespace WcfServiceLibrary1 
{ 
    public class DerivedFactory : ServiceHostFactory 
    { 
     protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
     { 
      ServiceHost host = new ServiceHost(serviceType, baseAddresses); 

      // do something here 

      return host; 
     } 
    } 
} 

然後,我添加了serviceHostingEnvironment部分在app.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 

<serviceHostingEnvironment> 
    <serviceActivations> 
    <add service="WcfServiceLibrary1.Service1" 
      relativeAddress="./MyService.svc" 
      factory="WcfServiceLibrary1.DerivedFactory"/> 
    </serviceActivations> 
</serviceHostingEnvironment> 

    <services> 
     <service name="WcfServiceLibrary1.Service1"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 

我把一個斷點的方法CreateServiceHost在DerivedFactory類。我執行的項目,但:http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/MyService.svc

我已經與relativeAddress =「/ MyService.svc測試:

  • 我不會在斷點
  • 我無法從瀏覽器訪問進入「和relativeAddress =」MyService.svc「,但這不起作用。

    有一種方法可以在調試模式下測試自定義工廠嗎?

    服務類:

    namespace WcfServiceLibrary1 
    { 
        public class Service1 : IService1 
        { 
         public string GetData(int value) 
         { 
          return string.Format("You entered: {0}", value); 
         } 
    
         public CompositeType GetDataUsingDataContract(CompositeType composite) 
         { 
          if (composite == null) 
          { 
           throw new ArgumentNullException("composite"); 
          } 
          if (composite.BoolValue) 
          { 
           composite.StringValue += "Suffix"; 
          } 
          return composite; 
         } 
        } 
    } 
    

回答

1

這種特定的答案很簡單,並參與冗餘配置。爲了讓我們更多地瞭解,我詳細闡述了我的答案。

通過託管服務達到你的服務的正確方法的IIS是下面的網址http://localhost:8733/MyService.svc
這是由

<add service="WcfServiceLibrary1.Service1" 
    relativeAddress="./MyService.svc" 
    factory="WcfServiceLibrary1.DerivedFactory"/> 

只有在自我託管授權服務您將需要baseAddress配置,因此它是多餘的並且可以被移除。

<host> 
    <baseAddresses> 
    <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" /> 
    </baseAddresses> 
</host> 

如果你想達到的詳細地址http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/您需要更改relativeAddress以下幾點:

relativeAddress="./Design_Time_Addresses/WcfServiceLibrary1/Service1/MyService.svc" 
+0

謝謝回答。 也許我的問題太簡單了,我只是想在調試模式下使用WCF服務主機(WcfSvcHost.exe)來測試我的自定義服務主機。 –

+0

當我重寫我的問題,我發現這個其他問題: (http://stackoverflow.com/questions/1543327/forcing-wcfsvchost-exe-to-use-my-custom-service-host) 我認爲這是不可能強制WcfSvcHost.exe使用我的自定義服務主機。 那麼,爲了調試我的解決方案,我必須使用自託管? –

+0

如果沒有爲您的服務進行適當的配置,則無法使用WCF服務主機。因此,我敦促您再次閱讀答案並對齊您的配置。 –