最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to deserializepass values from proxy service to data class - Stack Overflow

programmeradmin2浏览0评论

I'm running through a couple of tutourials on WCF and run into a snag with my own custom classes.

I have the following data class housed within a WCF service (Class Library):

[DataContract]
public class Hello
{
    [DataMember]
    public int HelloId { get; set; }
    [DataMember]
    public string HelloName { get; set; }
}

The service class is set up as follows:

public class HelloService : IHelloService
{
    public string GetMessage() => "Hi";

    public Hello GetHello() => new Hello() { HelloId = 1, HelloName= "Hi" };
}

This service is hosted by a basic console application:

static void Main(string[] args)
{
    using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService.HelloService))) // Both the solution and class go by the same name - HelloService
    {
        serviceHost.Open();
        Console.WriteLine("Host started @ " + DateTime.Now.ToString("HH:mm:ss"));
        Console.ReadLine();
    }
}

Then comes my problem. Next, I set up another windows form application to consume this hosted service. I connected to the service by adding it as a service reference. In the code behind, I set up a button click event handler like this:

private void button1_Click(object sender, EventArgs e)
{
    HelloService.HelloServiceClient helloServiceClient = new HelloService.HelloServiceClient("NetTcpBinding_IHelloService");
    label1.Text = helloServiceClient.GetMessage(textBox1.Text); // This works

    HelloClient.Hello foobar = (Hello)helloServiceClient.GetHello(); // This does not compile
 }

I understand that the client Hello data class is different from the connected service Hello data class, hence the error. I'd still like to know how to pass the contents over since I made sure that the property names, data types and ordering match up. Aside from manually assigning these one by one, is there no way of passing the values? I couldn't quite figure out how to get the DataContractSerializer or DataContractResolver to do the job.

I figure that if the string classes can pass the value implicitly, there must be an efficient way of doing so with other reference types.

发布评论

评论列表(0)

  1. 暂无评论