net.tcp在以编程方式设置时有效,但在使用配置文件时无效

时间:2016-06-23 18:57:45

标签: wcf wcf-binding

我尝试从以编程方式设置端点切换到使用配置文件。问题是,当我使用配置文件时,没有抛出错误,但是没有打开tcp端口,客户端无法连接。

这是netstat,显示在以编程方式设置时打开的端口,然后是配置文件。 netstat output

以下是我的完整示例客户端服务器和合同 的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>   
        <services>
            <service name="Server.CustomerService">
                <endpoint 
                    address="net.tcp://localhost:8081/CustomerService"
                    binding="netTcpBinding"
                    contract="Shared.ICustomerService"/>
            </service>
        </services>
    </system.serviceModel>
</configuration>

服务器:

class ProgramService
    {
        static List<Customer> _customers = new List<Customer>();
        static void Main(string[] args)
        {
            CreateCustomers();
            Uri netTCPObject = new Uri("net.tcp://localhost:8081/CustomerService");
            ServiceHost sessionHost = new ServiceHost(typeof(CustomerService), netTCPObject);
            //ServiceHost sessionHost = new ServiceHost("Server.CustomerService");  //using the app.config

            sessionHost.Open();

            Console.WriteLine("Service is running");
            Console.ReadLine();
            sessionHost.Close();
        }

        private static void CreateCustomers()
        {
            _customers.Add(new Customer() { CustomerId = 1, FirstName = "Fred", LastName = "Flintstone" });
            _customers.Add(new Customer() { CustomerId = 2, FirstName = "John", LastName = "Doe" });
            _customers.Add(new Customer() { CustomerId = 3, FirstName = "Rebecca", LastName = "Johndaughter" });
            _customers.Add(new Customer() { CustomerId = 4, FirstName = "Julie", LastName = "Herald" });
        }

        public class CustomerService : ICustomerService
        {
            public Customer GetCustomer(int customerId)
            {
                return _customers.FirstOrDefault(c => c.CustomerId == customerId);
            }

            public bool UpdateCustomer(Customer customer)
            {
                var curCust = _customers.FirstOrDefault(c => c.CustomerId == customer.CustomerId);
                if (curCust != null)
                {
                    curCust.FirstName = customer.FirstName;
                    curCust.LastName = customer.LastName;
                }
                else
                {
                    _customers.Add(customer);
                }
                return true;
            }
        }
    }

合同:

namespace Shared
{
    [ServiceContract()]
    public interface ICustomerService
    {
        [OperationContract]
        Customer GetCustomer(int customerId);

        [OperationContract]
        bool UpdateCustomer(Customer customer);
    }

    [DataContract]
    public class Customer
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int CustomerId { get; set; }
    }
}

客户端:

class ProgramClient
{
    static void Main(string[] args)
    {
        //http://stackoverflow.com/a/2943206/232226
        NetTcpBinding binding = new NetTcpBinding();
        EndpointAddress endpoint = new EndpointAddress("net.tcp://localhost:8081/CustomerService");
        ChannelFactory<ICustomerService> factory =  new ChannelFactory<ICustomerService>(binding, endpoint);

        ICustomerService service = factory.CreateChannel();
        for (int i = 1; i < 5; i++)
        {
            Customer customer = service.GetCustomer(i);
            Console.WriteLine(String.Format("  Customer {0} {1} received.", customer.FirstName, customer.LastName));
        }
        Console.ReadLine();
    }
}

1 个答案:

答案 0 :(得分:1)

ServiceHost构造函数的重载接受Type,另一个接受object,或者是服务类的类型或实例。

这意味着new ServiceHost("someString")将调用object重载,这将导致异常,因为string未实现服务。

您需要使用您的服务类型调用它:

var serviceHost = new ServiceHost(typeof(CustomerService))

在配置中,使用类型的full name

<service name="ServiceNamespace.ProgramService.CustomerService">