[WCF] 使用 svcutil.exe 產生合約 Client 程式碼

  • 7643
  • 0
  • WCF
  • 2016-01-21

[WCF] 使用 svcutil.exe 產生合約 Client 程式碼

svcutil.exe 的位置在

@Windows 8 x86 / .Net 4.0

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools

得依照不同的 .NET 版本 使用svcutil.exe

更詳細的內容請參考:

http://msdn.microsoft.com/zh-tw/library/aa347733

 

我用上篇的範例做演練 http://www.dotblogs.com.tw/yc421206/archive/2013/10/23/125276.aspx

操作步驟如下:

Step1.調用 svcutil.exe 產生 Client 程式碼

Step2.在用戶端調用 ServiceClient 類別

 

 


Step1.調用 svcutil.exe 產生程式碼

1.我先將 svcutil.exe 檔案 copy 到目標目錄

2.用管理員身份執行cmd

image

3.產生 Metadata

svcutil.exe /t:metadata Tako.MSMQ.Contract.dll

 

4.產生代理程式碼及設定檔

svcutil *.wsdl *.xsd /language:cs /out:generateProxy.cs /config:app.config

 

執行結果如下圖:

SNAGHTMLa067d7

 

產生出來的程式碼如下,不過我只需要 Service1Client 類別,其餘的只需要參考 Tako.MSMQ.Contract.dll 就會有了,觀察一下 Service1Client 類別的寫法其實也不難自己實作也是相當的容易

只要覆寫原本 4 個建構子即可

// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18051
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace Tako.MSMQ.Contract
{
    using System.Runtime.Serialization;
    
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="User", Namespace="http://schemas.datacontract.org/2004/07/Tako.MSMQ.Contract")]
    public partial class User : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        
        private int AgeField;
        
        private string NameField;
        
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int Age
        {
            get
            {
                return this.AgeField;
            }
            set
            {
                this.AgeField = value;
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string Name
        {
            get
            {
                return this.NameField;
            }
            set
            {
                this.NameField = value;
            }
        }
    }
}
 
 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IService1")]
public interface IService1
{
    
    [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://tempuri.org/IService1/SendUser")]
    void SendUser(Tako.MSMQ.Contract.User user);
    
    [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://tempuri.org/IService1/SendUser")]
    System.Threading.Tasks.Task SendUserAsync(Tako.MSMQ.Contract.User user);
}
 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IService1Channel : IService1, System.ServiceModel.IClientChannel
{
}
 
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
{
    
    public Service1Client()
    {
    }
    
    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {
    }
    
    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }
    
    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }
    
    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }
    
    public void SendUser(Tako.MSMQ.Contract.User user)
    {
        base.Channel.SendUser(user);
    }
    
    public System.Threading.Tasks.Task SendUserAsync(Tako.MSMQ.Contract.User user)
    {
        return base.Channel.SendUserAsync(user);
    }
}

 

Step2.在用戶端調用 ServiceClient 類別

1.把產生好的 generateProxy.cs 加入到 Client 專案

2.並換掉原本的 proxy 寫法

{
    //ChannelFactory<IService1> proxy = new ChannelFactory<IService1>("netMsmqBinding.Service1");
    //proxy.Open();
    //IService1 channel = proxy.CreateChannel();
    Service1Client proxy = new Service1Client();
label1:

    Console.Write("按任意鍵繼續,按ESC離開");
    var key = Console.ReadKey(true);

    if (key.Key == ConsoleKey.Escape)
    {
        Console.WriteLine("再會~!");
        proxy.Close();
        Console.ReadLine();
        return;
    }

    Console.WriteLine("");
    Console.Write("Name:");
    string name = Console.ReadLine();
    Console.Write("Age:");
    string age = Console.ReadLine();

    var user = new User() { Name = name, Age = int.Parse(age) };
    Console.WriteLine("傳送內容 : " + user);
    //channel.SendUser(user);
    proxy.SendUser(user);
    Console.WriteLine("傳送完成");
    goto label1;
}

 
運行結果如下:
SNAGHTMLb34649

 

文章出自:http://www.dotblogs.com.tw/yc421206/archive/2013/10/24/125428.aspx

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo