Upon completion of the first chapter a WCF service WCF client

When you need to communicate WCF service and client provides a rich API. API implemented by the processing Service.ServiceModel converted into XML type .NET then sends a message from the client to the server. You can use the API programming directly, or you can use the tool to generate a proxy class and configuration files. In this section, we will first explain how to use the code directly call the service, then we will use the tools to achieve this process. The former method uses less code does not use the profile. The latter methods are less dependent and better micro-control of when calling. Each solution has a lot of the best applicable.
Full use of code written a WCF client
Like a service endpoint must define the ABCs expose a WCF service interface on the network, a client must know the ABCs to access these services. Thus, when writing code to access the service endpoint, the ABCs of encoded program to the client application.
Endpoint address is simple - it is the message being sent to the address. Endpoint address formats are defined in the transport protocol bindings. Endpoint is exposed precise message endpoint binding mechanism defined. There are some pre-set WCF bindings, such as netTcpBinding, wsHttpBinding and basicHttpBinding. Contract defines the exact format of XML services understandable. A typical use of [the ServiceContract] and [the DataContract] code label class, interface definition, and the WCF sequence into the structural class of XML to the transmission line.
List 1.6 shows the code to call the service operations. Code defines the ABCs service endpoint so it can access the service on.
First, the client defines the interface it wants to call. This interface is defined share in the client and server. C # syntax definitions and XML or WSDL is very different, but the same semantics. That is, it accurately describes how to access the server, including the name and operational parameters. The client then generates a ChannelFactory class to create a channel, the transmitted ABCs. In this case, Address is an IIS server host address, binding is BasicHttpBinding, Contract is IStockService interface. Finally, the client generates a channel to establish communication with the server and calls a method on the server.
1.6 List WCF client using code implementation
Using the code and configuration files to write a client
Back in 2001, Visual Studio introduces the Add Service Reference (add a network service reference), used three words to distributed computing to simplify the main guarantee of a right-click. This is a good thing, because it provides an entry point to expand the vast majority of professional software developers to standards-based distributed computing. But in the distributed computing how easily accessible, it took a lot of important complex issues. Visual Studio 2008 for compatibility with ASMX and other network services and continue to support the Add Service Reference, but also introduce the use Add Service Reference (ASR) to support the WCF. Because WCF is protocol independent and support a range of serialization, and secure coding method, ASR provides great flexibility in terms of ease of administration, performance and security.
ASR characteristics of Visual Studio used to obtain metadata and generate proxy classes and configuration files from the WCF service, just as shown in the picture 1.4. Behind the picture, ASR call svcutil.exe, svcutil.exe call a service MEX endpoint to query all of its interfaces to generate a proxy class and configuration files. Proxy class for the client to access the service operates like a way to access a local class to be. WCF proxy class using the class to compile and understand SOAP messages based on contractual service endpoint definition. Configuration files are stored ABCs services.
Write a client and a service call requires two steps: First, the configuration file and generate a proxy class, and secondly, to write code using a proxy class to call the service. In Visual Studio 2008 using ASR, solutions need to browse to the following e-mail Add Service Reference and then select Add Service Reference in the context menu. This will run as a dialog box shown in picture 1.5.
Dialog call svcutil instance to use the language code generation project to create a proxy class. But also has generated <system.serviceModel> app.config to store the address of the node, binding, and contract information necessary to call the endpoint.
As an alternative to using ASR, you can also use svcutil.exe instance directly. svcutil.exe, in C: \ Program Files \ Microsoft SDKs \ Windows \ v6.0 \ bin directory, there are many options, and help documentation can be displayed on the command line by typing -h. This example receiving metadata input and output may be produced in many forms. Metadata can be generated from a DLL class, a WSDL file or by calling WS- Metadata to get the service running in. List 1.7 shows how to use svcutil.exe generate metadata from the list of services 1.4 and 1.5 definition.
List 1.7 svcutil.exe generate the client proxy class and configuration files
svcutil http://localhost:8000/EssentialWCF/mex 
          -config:app.config  
          -out:generatedProxy.cs 
   Irrespective of what technology is used to generate proxy and configuration file, svcutil.exe produce the same results. List 1.8 shows the configuration file. Note that the client configuration files than it depends generation of server configuration files a bit lengthy. This gives flexibility to the client overloads special properties, such as timeouts, caching and client support security certification. 
1.8 List generated by svcutil.exe app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name ="BasicHttpBinding_StockService" closeTimeout="00:01:00" openTimeout ="00:01:00"
                 receiveTimeout="00:01:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
                 hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="624288"
                 maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8"
                 transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32"
                        maxStringContentLength="8192"
                        maxArrayLength="16384"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="16384"
                        />
          <security mode="None">
            <transport clientCredentialType="None"
                       proxyCredentialType="None"
                       realm=""/>
            <message clientCredentialType="UserName"
                     algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8000/EssentialWCF"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_StockService"
                contract="StockService"
                name="BasicHttpBinding_StockService" />
    </client>
  </system.serviceModel>
</configuration>
  在配置文件和代理类生成了以后,调用一个请求-回复服务操作是非常简单的。代理类的名字就是在服务契约的名字后面加上一个"Client"。在列表1.4 和1.5中定义的服务的代理类的名字是StockServiceClient.客户端代码创建一个代理类的实例然后调用这个类中的方法。列表1.9显示了 相关代码。
列表1.9 调用服务操作的客户端代码
namespace EssentialWCF
{
    class Client
    {
        static void Main(string[] args)
        {
            StockServiceClient proxy = new StockServiceClient();
            double p = proxy.GetPrice("msft");
            Console.WriteLine("Price:{0}", p);
            proxy.Close();
        }
    }
}


============

转载自


作者: DanielWise
出处: http://www.cnblogs.com/danielWise/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

转载于:https://www.cnblogs.com/llbofchina/archive/2011/06/24/2089223.html

Guess you like

Origin blog.csdn.net/weixin_34270606/article/details/94206758