[英]How to configure net.tcp binding for client in WCF
我是WCF服務的新手,我使用WCF創建了一個相同的應用程序。 請參閱下面的基本代碼:
IService1.cs
:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
String WelComeMessage(String name);
}
Service1.svc.cs
:
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public String WelComeMessage(String name)
{
return String.Format("{0}, Welcome to http://www.csharptutorial.in", name);
}
}
和web.config
:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
我想讓這個服務net.tcp
綁定准備好了,任何人都可以幫我理解在哪里聲明net.tcp
綁定以及如何使用net.tcp
綁定來使用這個服務?
編輯1:
我更新了下面的服務器配置文件(在WCF服務配置文件中):
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="DemoWCF.Service1"
behaviorConfiguration="MyServiceBehavior">
<endpoint name="NetTCPBinding_IService1Client"
address="net.tcp://localhost:10109/Service1.svc"
binding="netTcpBinding"
bindingConfiguration="EndPointConfiguration"
contract="DemoWCF.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:10109/Service1" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NetTCPBinding_IService1" sendTimeout="00:01:00">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="65536" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
以下是客戶端代碼:
static void Main(string[] args)
{
try
{
Service1Client service1Client = new Service1Client("NetTCPBinding_IService1");
Console.WriteLine(service1Client.WelcomeMessage("Hello from net.tcp"));
Console.ReadLine();
}
catch (Exception ex)
{
throw ex;
}
}
但它引發了一個例外:
例外:您嘗試為不支持.Net Framing的服務創建通道。 您可能遇到HTTP端點。
內在例外:
{“預期記錄類型'PreambleAck',找到'72'。”}
編輯2:
我在IIS 7.5中托管了我的WCF服務,並使用edit binding ...選項添加了net.tcp綁定。 以及從命令行:
設置網站“DemoWCF” - + bindings。[protocol ='net.tcp',bindingInformation ='10108:*']
Web服務顯示運行完美。
但是在客戶端使用此WCF服務仍然是同樣的例外。
客戶代碼:
try
{
ServiceReference1.Service1Client s = new ServiceReference1.Service1Client();
Response.Write(s.WelcomeMessage("Hello from net.tcp"));
Response.End();
}
catch (Exception ex)
{
Response.Write(ex);
Response.End();
}
客戶端Web.config:
<bindings>
<netTcpBinding>
<binding name="NetTCPBinding_IService1Client">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:10108/Service1.svc" binding="netTcpBinding"
bindingConfiguration="NetTCPBinding_IService1Client" contract="ServiceReference1.IService1"
name="NetTCPBinding_IService1Client" />
</client>
相同例外:您已嘗試為不支持.Net Framing的服務創建通道。 您可能遇到HTTP端點。
內在例外:
{“預期記錄類型'PreambleAck',找到'72'。”}
編輯3:
嘗試通過以下代碼消費服務:
IService1 vService;
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
EndpointAddress vEndPoint = new EndpointAddress("net.tcp://localhost:10108/Service1/");
ChannelFactory<IService1> cf = new ChannelFactory<IService1>(b, vEndPoint);
vService = cf.CreateChannel();
try
{
Response.Write("Result from the system " + vService.WelcomeMessage("Hello"));
}
catch (Exception ex)
{
Response.Write("Error Occured while connection establish to " + vEndPoint.Uri.ToString());
}
Response.End();
仍然得到同樣的例外,任何人都可以幫忙嗎?
你需要類似的東西
<system.serviceModel>
<services>
<service name="MyApp.Service1"
behaviorConfiguration="MyServiceBehavior">
<endpoint name="MyEndPoint"
address="net.tcp://localhost:9000/Service1"
binding="netTcpBinding"
bindingConfiguration="EndPointConfiguration"
contract="MyApp.IService1" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9000/Service1" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="True" />
<dataContractSerializer maxItemsInObjectGraph="65536" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="EndPointConfiguration"
sendTimeout="00:01:00">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
'MyApp.Service1' - 表示服務的全名,包括名稱空間。 'MyApp.IService1' - 表示服務接口的全名,包括服務實現的名稱空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.