首页 文章

如何使用WCF服务

提问于
浏览
0

我是C#开发的新手,作为我分配了WCF服务的第一项任务,并在代码项目中尝试了一些示例 . 现在我正在使用复杂类型,但无法得到响应 . 从wsdl文件和xsd使用svcutil.exe wsdlname xsd并获得两个基于这些文件的文件在本地创建一个服务并尝试以下面的方式使用 .

以下是界面

我能够运行此服务,能够查看服务URL并能够在我的客户端中引用 .

下面是我试图从客户端调用服务到这个存根,但无法得到想法,如何调用

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                 ServiceReference1.sendMessageResponse1 s = new
ServiceReference1.sendMessageResponse1();
                 ServiceReference1.sendMessageResponse1 s1 = new ServiceReference1.sendMessageResponse1();
                 //s1.messageid = 1;
                 //s1.recipient = "Chiranjeevi";
                 //s1.status = "Sent";
                 //ServiceReference1.sendMessageResponse ss= (ServiceReference1.sendMessageResponse) s1;
                 Console.Read();            
             }
        }
    }

但是在尝试调用服务时,我也没有为默认构造函数提供任何输出 . 尝试使用console.writeline();

我试图调用sendMessageResponse1 sendMessage(sendMessageRequest请求);从服务 .

得到以下错误,同时尝试调用上述方法 . 错误1无法将类型'ConsoleApplication3.ServiceReference1.sendMessageResponse1'转换为'ConsoleApplication3.ServiceReference1.sendMessageResponse'

2 回答

  • 1

    好吧,您正在尝试将一个类的实例转换为另一个类的实例 .

    ServiceReference1.sendMessageResponse1 s = 
        new ServiceReference1.sendMessageResponse1();
    ServiceReference1.sendMessageResponse1 s1 = 
        new ServiceReference1.sendMessageResponse1();
    
    //s1.messageid = 1;
    //s1.recipient = "Chiranjeevi";
    //s1.status = "Sent";
    //ServiceReference1.sendMessageResponse ss=
    //    (ServiceReference1.sendMessageResponse) s1;
    

    以下代码行将中断,因为类 sendMessageRequestsendMessageRequest1 之间没有转换 .

    ServiceReference1.sendMessageResponse ss=
        (ServiceReference1.sendMessageResponse) s1;
    

    这是交易:

    • s1 的类型为 sendMessageResponse1 .

    • 您试图将其转换为 sendMessageResponse 类型 .

    • 那不是来自后者的't going to work, because the former isn'吨 .

    这正是错误告诉你的:

    错误1无法将类型'ConsoleApplication3.ServiceReference1.sendMessageResponse1'转换为'ConsoleApplication3.ServiceReference1.sendMessageResponse'

  • 0

    经过大量的考虑后得到了以下网址,这是最好的初学者,他们直接参与了WCF的知识 . 最佳起点文章 .

    https://www.packtpub.com/books/content/implementing-wcf-service-real-world

相关问题