簡體   English   中英

在IIS托管的代碼中使用NetTcp綁定和配置來托管WCF服務

[英]Host WCF service using NetTcp binding and configuration in code hosted in IIS

更新以下問題可能是由於net.tcp在IIS express上無法使用。 好的答案(最好是示例)仍然非常受歡迎。

我正在嘗試在IIS中托管WCF服務,而不使用web.config文件( 有關該基礎知識的msdn文檔 )。 我想使用會話和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" %>

根據我的所作所為,我有很多不同的例外。

我自己找到了答案。 首先, 我發現您不能在IIS express中使用net.tcp綁定 另外,還需要在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 });
    }

似乎沒有太多的相關文檔。 我必須查看一些Microsoft的源代碼才能找到有關元數據綁定行為的特殊部分。

如果要在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM