如何在WCF中为客户端配置net.tcp绑定

 手机用户2502871803 发布于 2023-02-08 11:22

我是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:


  
    
  
  
    
      
        
          
          
        
      
    
    
  
 
    
  

我想让这个服务net.tcp绑定准备好,任何人都可以帮助我理解在哪里声明net.tcp绑定以及如何使用net.tcp绑定来使用此服务?

编辑1:

我更新了下面的服务器配置文件(在WCF服务配置文件中):



  
    
  
  
    
      
        
        
        
          
            
          
        
      
    

    
      
        
          
        
      
    

    
      
        
          
          
          
        
      
    
    
  
  
    
  


以下是客户端代码:

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:


  
    
      
    
  


  

相同例外:您已尝试为不支持.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 cf = new ChannelFactory(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();

仍然得到同样的例外,任何人都可以帮忙吗?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有