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" %>
我已經得到了很多不同的例外,這取決於我做了什麼。