این مقاله، چهارمین مرحله از شش مرحله ای است که باید برای ایجاد یک سرویس Windows Communication Foundation یا (WCF) و یک client که سرویس را فراخوانی می کند، انجام داد.
این مقاله، چگونگی بازیابی metadata از یک سرویس WCF و استفاده از آن برای ایجاد یک پروکسی WCF که می تواند به سرویس دسترسی داشته باشد را توصیف می کند. این مرحله با استفاده از ServiceModel Metadata Utility Tool (Svcutil.exe) که توسط WCF فراهم می شود، تکمیل می شود. این ابزار metadata را از سرویس بدست می آورد و یک فایل کد مدیریت شده امن برای پروکسی در زبانی انتخابی شما، generate می کند. این ابزار علاوه بر ایجاد client proxy، فایل پیکربندی برای client ایجاد می کند که client application را قادر به وصل شدن به سرویس در یکی از endpointهایش می کند.
client application از پروکسی generate شده برای ایجاد یک WCF client object استفاده می کند.
ایجاد یک Windows Communication Foundation client
1. با انجام مراحل زیر، یک پروژه جدید در solution کنونی برای client در Visual Studio 2010 ایجاد کنید.
- در Solution Explorer، داخل همان solution که سرویس درون آن است، روی solution کنونی کلیک راست کنید و Add و سپس New project را انتخاب کنید.
- در پنجره Add New Project، Visual Basic یا Visual C# را انتخاب کنید، و سپس Console Application را انتخاب کنید و نام آن را Client بگذارید. از Location پیش فرض استفاده کنید.
- روی OK کلیک کنید.
2. یک reference به System.ServiceModel.dll برای پروژه اضافه کنید..
- در زیر پروژه client در Solution Explorer روی پوشه References راست کلیک کنید و Add Reference را انتخاب کنید.
- تب .NET و سپس System.ServiceModel.dll (نسخه 4.0.0.0) را از لیست انتخاب کنید و OK کنید.
3. یک عبارت using (Imports در VB) برای فضای نامی System.ServiceModel در فایل generate شده Program.cs یا Program.vb اضافه کنید.
در VB
Imports System.ServiceModel
در #C
using System.ServiceModel;
4. در Visual Studio، دکمه F5 را بزنید تا سرویس ایجاد شده در مرحله قبلی را شروع کنید.
ServiceModel Metadata Utility Tool (Svcutil.exe) .5. را با switchهای مناسب اجرا کنید تا کد client و فایل پیکربندی را با انجام مراحل زیر، ایجاد کنید.
- در منوی start، روی All Programs و سپس Visual Studio 2010 کلیک کنید. روی Visual Studio Tools و سپس Visual Studio 2010 Command Prompt کلیک کنید.
- به دایرکتوری که می خواهید کد client را قرار دهید بروید. اگر پروژه client را به طور پیش فرض ایجاد کرده اید، دایرکتوری، C:\Users\<user name>\My Documents\Visual Studio 10\Projects\Service\Client است.
- از ابزار خط فرمان ServiceModel Metadata Utility Tool (Svcutil.exe) با switchهای مناسب استفاده کنید تا کد client را ایجاد کنید. مثال زیر، یک فایل کد و یک فایل پیکر بندی برای سرویس generate می کند.
در VB
svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config
http://localhost:8000/ServiceModelSamples/service
در #C
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config
http://localhost:8000/ServiceModelSamples/service
به طور پیش فرض، کد پروکسی client، در فایلی که بعد از سرویس نام گذاری می شود، generate می شود. سوییچ /out، نام فایل client proxy را به GeneratedProxy.cs تغیییر می دهد. سوییچ /config، نام فایل پیکربندی client را از Output.config به App.config تغییر می دهد. به خاطر داشته باشید که هردوی این فایل ها در دایرکتوری C:\Users\<user name>\My Documents\Visual Studio 10\Projects\Service\Client ایجاد می شوند.
6. پروکسی generate شده را به client project در Visual Studio اضافه کنید، روی client project در Solution Explorer کلیک راست کنید و Add و سپس Existing Item را انتخاب کنید. فایل generatedProxy را که در مرحله قبل generate شده است را انتخاب کنید.
مثال
کد زیر کد client را که توسط ServiceModel Metadata Utility Tool (Svcutil.exe). ایجاد شده است، نشان می دهد.
در VB
'----------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.1366
'
' Changes to this file may cause incorrect behavior and
will be lost if
' the code is regenerated.
' </auto-generated>
'-----------------------------------------------------------------
Option Strict Off
Option Explicit On
<System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute
([Namespace]:="http://Microsoft.ServiceModel.Samples",
ConfigurationName:="ICalculator")> _
Public Interface ICalculator
<System.ServiceModel.OperationContractAttribute
(Action:=
"http://Microsoft.ServiceModel.
Samples/ICalculator/Add",
ReplyAction:=
"http://Microsoft.ServiceModel.
Samples/ICalculator/AddResponse")> _
Function Add(ByVal n1 As Double,
ByVal n2 As Double) As Double
<System.ServiceModel.OperationContractAttribute
(Action:="http://Microsoft.ServiceModel.
Samples/ICalculator/Subtract", ReplyAction:=
"http://Microsoft.ServiceModel.
Samples/ICalculator/SubtractResponse")> _
Function Subtract(ByVal n1 As Double,
ByVal n2 As Double) As Double
<System.ServiceModel.OperationContractAttribute
(Action:="http://Microsoft.ServiceModel.
Samples/ICalculator/Multiply", ReplyAction:=
"http://Microsoft.ServiceModel.
Samples/ICalculator/MultiplyResponse")> _
Function Multiply(ByVal n1 As Double,
ByVal n2 As Double) As Double
<System.ServiceModel.OperationContractAttribute
(Action:="http://Microsoft.ServiceModel.
Samples/ICalculator/Divide", ReplyAction:=
"http://Microsoft.ServiceModel.
Samples/ICalculator/DivideResponse")> _
Function Divide(ByVal n1 As Double,
ByVal n2 As Double) As Double
End Interface
<System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0")> _
Public Interface ICalculatorChannel
Inherits ICalculator, System.
ServiceModel.IClientChannel
End Interface
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0")> _
Partial Public Class CalculatorClient
Inherits System.ServiceModel.ClientBase(Of ICalculator)
Implements ICalculator
Public Sub New()
MyBase.New
End Sub
Public Sub New(ByVal endpointConfigurationName As String)
MyBase.New(endpointConfigurationName)
End Sub
Public Sub New(ByVal endpointConfigurationName As String,
ByVal remoteAddress As String)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal endpointConfigurationName As String,
ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal binding As System.
ServiceModel.Channels.Binding,
ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(binding, remoteAddress)
End Sub
Public Function Add(ByVal n1 As Double,
ByVal n2 As Double)
As Double Implements ICalculator.Add
Return MyBase.Channel.Add(n1, n2)
End Function
Public Function Subtract(ByVal n1 As Double,
ByVal n2 As Double)
As Double Implements ICalculator.Subtract
Return MyBase.Channel.Subtract(n1, n2)
End Function
Public Function Multiply(ByVal n1 As Double,
ByVal n2 As Double)
As Double Implements ICalculator.Multiply
Return MyBase.Channel.Multiply(n1, n2)
End Function
Public Function Divide(ByVal n1 As Double,
ByVal n2 As Double)
As Double Implements ICalculator.Divide
Return MyBase.Channel.Divide(n1, n2)
End Function
End Class
در #C
//--------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1366
//
// Changes to this file may cause incorrect behavior
and will be lost if
// the code is regenerated.
// </auto-generated>
//-------------------------------------------------------------------
[System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute
(Namespace="http://Microsoft.ServiceModel.Samples",
ConfigurationName="ICalculator")]
public interface ICalculator
{
[System.ServiceModel.OperationContractAttribute
(Action="http://Microsoft.ServiceModel.
Samples/ICalculator/Add", ReplyAction=
"http://Microsoft.ServiceModel.
Samples/ICalculator/AddResponse")]
double Add(double n1, double n2);
[System.ServiceModel.OperationContractAttribute
(Action="http://Microsoft.ServiceModel.
Samples/ICalculator/Subtract", ReplyAction=
"http://Microsoft.ServiceModel.
Samples/ICalculator/SubtractResponse")]
double Subtract(double n1, double n2);
[System.ServiceModel.OperationContractAttribute
(Action="http://Microsoft.ServiceModel.
Samples/ICalculator/Multiply", ReplyAction=
"http://Microsoft.ServiceModel.
Samples/ICalculator/MultiplyResponse")]
double Multiply(double n1, double n2);
[System.ServiceModel.OperationContractAttribute
(Action="http://Microsoft.ServiceModel.
Samples/ICalculator/Divide", ReplyAction=
"http://Microsoft.ServiceModel.
Samples/ICalculator/DivideResponse")]
double Divide(double n1, double n2);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0")]
public interface ICalculatorChannel :
ICalculator, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute
("System.ServiceModel", "3.0.0.0")]
public partial class CalculatorClient :
System.ServiceModel.ClientBase<ICalculator>, ICalculator
{
public CalculatorClient()
{
}
public CalculatorClient
(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public CalculatorClient
(string endpointConfigurationName,
string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public CalculatorClient
(string endpointConfigurationName,
System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public CalculatorClient
(System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public double Add(double n1, double n2)
{
return base.Channel.Add(n1, n2);
}
public double Subtract(double n1, double n2)
{
return base.Channel.Subtract(n1, n2);
}
public double Multiply(double n1, double n2)
{
return base.Channel.Multiply(n1, n2);
}
public double Divide(double n1, double n2)
{
return base.Channel.Divide(n1, n2);
}
}
الان، شما یک Windows Communication Foundation client ایجاد کرده اید.