在IIS

时间:2016-06-10 07:27:34

标签: c# wcf configuration nettcpbinding

更新以下问题可能是由于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" %>

根据我的所作所为,我得到了很多不同的例外。

1 个答案:

答案 0 :(得分: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 });
    }

似乎没有太多关于此的文档。我必须查看一些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>