【问题标题】:Host WCF service using NetTcp binding and configuration in code hosted in IIS在 IIS 中托管的代码中使用 NetTcp 绑定和配置托管 WCF 服务
【发布时间】:2016-10-11 01:46:58
【问题描述】:

更新 以下问题可能是由于 net.tcp 在 IIS express 上不可用。好的答案(最好是例子)仍然很受欢迎。

我试图在不使用 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" %>

根据我所做的不同,我遇到了很多不同的例外情况。

【问题讨论】:

    标签: c# wcf configuration nettcpbinding


    【解决方案1】:

    我自己找到了答案。首先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 });
        }
    

    似乎没有太多关于此的文档。为了找到有关元数据绑定行为的特殊部分,我不得不查看一些 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 2015-01-28
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 2020-04-26
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多