c# - 如何在 WCF 中为客户端配置 net.tcp 绑定(bind)

标签 c# .net wcf net.tcp

我是 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 绑定(bind)准备好,任何人都可以帮助我了解在哪里声明 net.tcp 绑定(bind)以及如何使用这个服务net.tcp 绑定(bind)?

编辑 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 的服务 channel 。您可能遇到了 HTTP 端点。

内部异常:

{“预期记录类型‘PreambleAck’,发现‘72’。”

编辑 2:

我在 IIS 7.5 中托管了我的 WCF 服务,并使用编辑绑定(bind)...选项添加了 net.tcp 绑定(bind)。以及命令行:

设置站点“DemoWCF”-+bindings.[protocol='net.tcp',bindingInformation='10108:*']

Web 服务显示完美运行。

enter image description here

但在客户端使用此 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 的服务 channel 。您可能遇到了 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' - 表示服务接口(interface)的全名,包括服务实现的命名空间。

关于c# - 如何在 WCF 中为客户端配置 net.tcp 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20770958/

相关文章:

c# - 在 .NET 中提取字符串

c# - 在 MVVM 中实现 ICommand 时,我遗漏了一些东西

c# - 查询sql数据库

c# - 如何使用 XPathNavigator 识别 XPath 1.0 中的重复节点以进行评估?

c# - 如何在 .NET 中制作键盘快捷键

java - 请求被 CDATA 包裹

c# - 分隔线拖放树节点 c# WinForms

.net - 将对象设置为空

c# - WCF - 在 CMS 中使用 Web 控制的客户端安全

.net - 唤醒 Windows 服务中运行的 WCF 服务