如何在WCF中生成客户端类 [英] How to generate a client class in wcf

查看:54
本文介绍了如何在WCF中生成客户端类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人都好,

我是WCF的新手.我不知道如何创建客户端类的想法.我在service1.cs中实现了一些方法.然后,我将此命令放入命令提示符"svcutil.exe http://localhost: 53391/Service.svc?wsdl.但是错误来了.请告诉我oto create client class这个概念

Iservice1.cs

Hai to all,

I am new to WCF.I don''t knew the idea on how to create a client class.I implement some method in service1.cs.Then afterwards i put this command in command prompt "svcutil.exe http://localhost:53391/Service.svc?wsdl".But the error is coming.Plz tell me the concept oto create client class

Iservice1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService3
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(string value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

service1.svc.cs

service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService3
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

推荐答案

在尝试执行代理生成时,您需要确保服务正在运行.
根据生成服务器生成的URL http://localhost:53391/Service.svc?wsdl判断.如果是这样,请确保Cassini(与VS捆绑在一起的轻量级Web服务器)正在运行,可以通过在Web浏览器中打开URL进行检查:您应该获得wsdl文件.

如果它是同一解决方案的一部分,则可以直接在客户端代码中引用该服务,在这种情况下,将为您生成代理,并且它们会与服务更改保持最新.

如果以上方法均无济于事,您可以使用此错误消息.
You''ll need to make sure the service is running when attempting to execute the proxy generation.
Judging by the URL http://localhost:53391/Service.svc?wsdl you are generating against the build server. If so make sure Cassini (the lightweight web server bundled with VS) is running, you can check this by opening the URL in a web browser: you should get the wsdl file.

You can directly reference the Service in the client code if it is part of the same solution, in this case the proxies are generated for you, and they keep up to date with service changes.

If none of this helps, can you give use the error message.


这篇关于如何在WCF中生成客户端类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆