2016-06-10 48 views
0

UPDATE以下問題可能是由於net.tcp在IIS Express上不可用。好的答案(最好是例子)仍然非常受歡迎。在託管在IIS中的代碼中使用NetTcp綁定和配置的主機WCF服務

我想在不使用web.config文件(msdn documentation on the basics on that)的情況下在IIS中託管WCF服務。我想使用會話和NetTcpBinding,但我似乎遇到了讓元數據工作的問題。我嘗試了很多東西。這是我現在的代碼:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
public class InternalInternalESensService : IInternalESensService 
{ 
    public static void Configure(System.ServiceModel.ServiceConfiguration config) 
    { 
     NetTcpBinding wsBind = new NetTcpBinding(SecurityMode.Transport); 
     config.AddServiceEndpoint(typeof(IInternalESensService), wsBind, "net.tcp://localhost:42893/ESens/ESensInternalService.svc"); 
     // config.AddServiceEndpoint(typeof(IInternalESensService), basic, "basic"); 
     // config.Description.Endpoints.Add(new ServiceMetadataEndpoint(MetadataExchangeBindings.CreateMexHttpBinding(), new EndpointAddress(config.BaseAddresses[0]))); 
     config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetBinding = MetadataExchangeBindings.CreateMexHttpBinding()}); 
     //config.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); 
     config.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }); 
    } 

    public string Test(string input) 
    { 
     return "Hello " + input; 
    } 

未註釋的代碼顯示了我的一些絕望嘗試沒有工作。它實現了接口:

[ServiceContract(Name = "ESensInternalService", Namespace = Constants.WebserviceNameSpace + "/ESensService", SessionMode = SessionMode.Required)] 
public interface IInternalESensService 
{ 
    [OperationContract] 
    string Test(string input); 

接口和類實現中還有一些方法,但它們與問題/問題無關。

要在IIS中託管它,我使用svc文件。內容看起來像這樣:

<%@ ServiceHost Language="C#" Debug="true" Service="Esens.Integration.WebService.InternalInternalESensService" %> 

我已經得到了很多不同的例外,這取決於我做了什麼。

回答

0

我自己找到了答案。首先是I found that you cannot use net.tcp binding in IIS express。它也需要是(普通)enabled on the IIS

然後,它可以配置方式如下:

public static void Configure(System.ServiceModel.ServiceConfiguration config) 
    { 
     Uri netTcpAddress = config.BaseAddresses.FirstOrDefault(x => x.Scheme == Uri.UriSchemeNetTcp); 
     if (netTcpAddress == null) 
     { 
      throw new InvalidOperationException("No base address matches the endpoint binding net.tcp"); 
     } 

     Uri metaAddress = config.BaseAddresses.FirstOrDefault(x => x.Scheme == Uri.UriSchemeHttp); 
     if (metaAddress == null) 
     { 
      throw new InvalidOperationException("No base address matches the endpoint binding http used for metadata"); 
     } 

     config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); 

     NetTcpBinding wsBind = new NetTcpBinding(SecurityMode.Transport); 
     ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IInternalESensService)), wsBind, new EndpointAddress(netTcpAddress)); 
     config.AddServiceEndpoint(endpoint); 

     Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding(); 
     ContractDescription contractDescription = ContractDescription.GetContract(typeof(IMetadataExchange)); 
     contractDescription.Behaviors.Add(new ServiceMetadataContractBehavior(true)); 
     ServiceEndpoint mexEndpoint = new ServiceEndpoint(contractDescription, mexBinding, new EndpointAddress(metaAddress)); 
     mexEndpoint.Name = "mexTest"; 
     config.AddServiceEndpoint(mexEndpoint); 

     config.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }); 
    } 

似乎沒有太多的文檔這一點。爲了找到關於元數據綁定行爲的特殊部分,我必須查看一些微軟的源代碼。

如果你想要做同樣在web.config中它會是這個樣子:

<service behaviorConfiguration="StandardBehaviour" name="Mercell.Esens.Integration.WebService.InternalInternalESensService"> 
    <endpoint binding="netTcpBinding" 
    name="test" contract="Mercell.Esens.Integration.WebService.IInternalESensService" /> 
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" 
    name="Metadata" contract="IMetadataExchange" /> 
    </service> 

隨着行爲:

<behavior name="StandardBehaviour"> 
<serviceMetadata httpGetEnabled="true" /> 
</behavior> 
相關問題