你的位置:首页 > ASP.net教程

[ASP.net教程]WCF学习系列二


http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html

WCF Interview Questions – Part 2

This WCF tutorial is part-2 in series of WCF Interview Questions. Other parts in this series can be found here.

这是WCF问答系列教程中的第二部分,其他部分可以在下面的链接中找到:

  1. WCF Service Interview Questions – Part 1
  2. WCF Service Interview Questions – Part 2
  3. WCF Service Interview Questions – Part 3
  4. WCF Service Interview Questions – Part 4

MCSD Exam: 70-487

WCF Interview Questions List – Part 2【WCF问答系列教程二】

  1. What are the different ways to expose WCF Metadata?【有哪些方式来揭露WCF元数据?】
  2. What is mexHttpBinding in WCF?【WCF中的mexHttpBinding是什么?】
  3. What is a Service Proxy in Windows Communication Foundation?【WCF中的服务代理是什么?】
  4. What are the different ways to generate proxy in WCF?【在WCF中,都有哪些不同的方式来生成代理】
  5. Difference between using ChannelFactory and Proxies in WCF?【在WCF中使用ChannelFactory和Proxies的不同之处是?】
  6. How to create proxy for Non-WCF Services?【如何为Non-WCF服务创建代理?】
  7. Briefly explain Automatic Activation in WCF?【简短的解释一下,WCF中的自动激活?】
  8. What are the different WCF Instance Activation Methods available?【不同的WCF实例激活方法是?】
  9. What are the different ways to handle concurrency in WCF?【在WCF中,处理并发性都有哪些方式?】
  10. What is WCF throttling?【什么是WCF节流?】

What are the different ways to expose WCF Metadata?【有哪些方式来揭露WCF元数据?】

By default, WCF doesn’t expose metadata. We can expose it by choosing one of the following ways:

默认情况下,WCF并不暴露元数据。我们可以通过选择下面的方式之一,来实现:
1.    In configuration file, by enabling metadata exchange as follows:

1.在配置文件中,如下图所示,允许元数据交换:

Expose WCF Service Metadata
2.  ServiceHost can expose a metadata exchange endpoint to access metadata at runtime.

2.ServiceHost 可以在运行的时候,暴露一个元数据交换节点来访问元数据。

   using (ServiceHost host = new ServiceHost(typeof(WcfService1)))
   {
              ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
              behavior.HttpGetEnabled = true;
              host.Description.Behaviors.Add(behavior);
              host.Open();
              Console.WriteLine(“My Service here……….”);             
      Console.ReadLine();
              host.Close();
   }

 

What is mexHttpBinding in WCF?【WCF中的mexHttpBinding是什么?】

In order to generate proxy, we need service metadata and mexHttpBinding is the binding that returns service metadata.

为了能够生成代理,我们需要服务元数据,mexHttpBinding 就是这个绑定,它给我们返回服务元数据。

If we look into our configuration file, service will have an endpoint with mexHttpBinding as follows:

如果我们进去配置文件,就会发现,服务有一个mexHttpBinding 节点,如下图:

mexHttpBinding

and service metadata behavior will be configured as follows:【并且服务元数据行为将会被配置如下:】httpGetEnabled

Before deployment of application to production machine, it should be disabled.【在部署应用程序到生产的机器之前,它应该被禁用。】
In order to support other protocols, related bindings are:【为了能够支持其他的协议,相关的绑定如下:】

  • mexHttpBinding
  • mexHttpsBinding
  • mexTcpBinding

What is a Service Proxy in Windows Communication Foundation?【WCF中的服务代理是什么?】

A service proxy or simply proxy in WCF enables application(s) to interact with WCF Service by sending and receiving messages. It’s basically a class that encapsulates service details i.e. service path, service implementation technology, platform and communication protocol etc. It contains all the methods of service contract (signature only, not the implementation). So, when the application interact the service through proxy, it gives the impression that it’s communicating a local object.

  WCF中,一个服务代理或者一个简单的代理,能够确保应用程序和WCF服务,通过发送和接收消息来进行交互。它基本上是一个类,封装服务细节,即,服务路径,服务实现的技术,平台和通信协议等等。它包含所有服务契约的方法(只有签名,没有实现)。所以当应用程序,通过代理和服务进行交互的时候,这会给人一种印象:这是和本地对象在通信。


WCF Service ProxyWe can create proxy for a service by using Visual Studio or SvcUtil.exe.【我们可以使用Visual Studio或者SvcUtil.exe为服务创建代理。】

What are the different ways to generate proxy in WCF?【在WCF中,都有哪些不同的方式来生成代理】

Generating proxy using Visual Studio is simple and straight forward.【使用Visual Studio生成代理是简单而直截了当的】

  • Right click References and choose “Add Service Reference”.【右键“引用”,选择“添加服务引用”】
  • Provide base address of the service on “Add Service Reference” dialog box and click “Go” button. Service will be listed below.【在点击“添加服务引用”之后,弹出了,提供了基于地址的服务对话框,输入相应的服务地址之后,点击“前往”,服务将会在下面列出来。】
  • Provide namespace and click OK.【输入或者就让它是默认的命名空间名称,然后点击“确定”】

Visual studio will generate a proxy automatically.【Visual studio将会自动的生成一个代理。】

We can generate proxy using svcutil.exe utility using command line. This utility requires few parameters like HTTP-GET address or the metadata exchange endpoint address and a proxy filename i.e. optional.

我们可以使用svcutil.exe工具,通过使用命令行来生成代理。这个工具需要一些参数(HTTP-GET address或者元数据交换的节点地址,还有一个代理的文件名。)
svcutil http://localhost/MyService/Service1.svc /out:MyServiceProxy.cs

If we are hosting the service at a different port(other than default for IIS which is 80), we need to provide port number in base address.

如果我们把服务托管在不同的端口(而不是IIS默认的80端口的话,)我们需要在基地址中提供端口号。
svcutil http://localhost:8080/MyService/Service1.svc /out:MyServiceProxy.cs
For parameter details regarding svcutil, please follow the MSDN link【关于svcutil参数细节,请看下面的MSDN链接】
http://msdn.microsoft.com/en-us/library/aa347733.aspx

Please follow here for a complete detailed WCF Tutorial on creating Service Proxy

点击这里, follow here ,获取,创建完整的,详细的服务代理WCF教程

Difference between using ChannelFactory and Proxies in WCF?【在WCF中使用ChannelFactory和Proxies的不同之处是?】

A ChannelFactory creates a kind of Channel used by clients to communicate with service endpoints. If we have control over Server and Client, then ChannelFactory is a good option because it relies on having local interfaces that actually describes the service i.e. service contract.

      ChannelFactory 创建一种客户端和服务端节点,用来通信的通道。如果我们可以控制服务端和客户端,那么ChannelFactory 是一个不错的选择,因为它依赖的本地接口,实际上描述了这个服务,即服务契约。

On the other hand, If we don’t have control over server and only have WSDL/URL, then it’s better to generate proxy using Visual Studio or SvcUtil. SvcUtil is better option as compared to Visual Studio because we have more control in case of SvcUtil.

     另外一方面,如果我们没有控制服务端,并且仅仅只是有WSDL/URL,那么最好的方式是,通过Visual Studio 或者 SvcUtil生产代理。和Visual Studio相比, SvcUtil 是一个更好的选择,因为我们在SvcUtil 中有更多的控制权。

How to create proxy for Non-WCF Services?【如何为Non-WCF服务创建代理?】

In case of Non-WCF Services, we can create proxy by either using Visual Studio or svcUtil.exe tool by pointing to WSDL of the non-WCF service. In this scenario, we can’t create proxy through ChannelFactory or manually developing proxy class because we don’t have local interfaces i.e. service contract.

     在没有WCF的服务中,我们使用Visual Studio 或者 svcUtil.exe工具,通过指向这个 non-WCF服务的WSDL,来创建代理。在这个场景下,我们不能通过ChannelFactory 或者手动来开发代理类,来创建代理,因为我们没有本地的接口,也就是服务契约。

 

Breifly explain Automatic Activation in WCF?【简短的解释一下,WCF中的自动激活?】

Automatic activation means service starts and serves the request when a message request is received, but service doesn’t need to be running in advance.【自动激活,意味着:当服务收到请求消息的时候,启动和响应这个请求的过程,并不需要服务,提前运行。】

Both IIS (Internet Information Services) and WAS (Windows Activation Service) supports automatic activation. It means if your service is hosted in IIS or WAS, then it will be activated automatically as a new message
arrives. But there are few scenarios in which service needs to be running in advance, for example, in case of Self-
Hosting.Automatic Activation in WCF

Note: WAS (Windows Activation Service) is a process activation mechanism introduced in IIS 7.0 that supports other protocols (e.g. TCP, NamedPipes etc.) along with existing HTTP.

IIS(Internet Information Services)和WAS (Windows Activation Service)都支持自动激活。它意味着,如果你的服务托管在IIS或者WAS中,当新消息来的时候,服务就会自动的激活。但是,有少数的情况下,你的服务需要提前运行,例如自我托管的情况下。

注意:WAS (Windows Activation Service)是在IIS 7.0中引进来的进程激活机制。它支持其他的协议(例如 TCP,NamedPipes 等等),以及现有的HTTP。

If you want to go into details and see the implementation of different available hosting options in WCF, you can follow the below WCF Service Hosting Tutorials.

【如果你想了解更多细节,了解WCF中不同的实现托管服务的方式,你可以看看下面的系列教程:】

  • WCF Console Hosting【WCF托管在控制台中】
  • Hosting in Windows Service【WCF托管在Windows服务中】
  • Hosting in IIS (Internet Information Service)【WCF托管在IIS中】
  • WCF WAS (Windows Activation Service) Hosting【WCF托管在WAS中】

 

What are the different WCF Instance Activation Methods available?【不同的WCF实例激活方法是?】

WCF supports three different types of Instance Activation methods:【WCF支持三种不同的方式的实例激活方法:】

  • Per Call: A new instance is created against each incoming request from client and later disposed off  as 
  • response generated.WCF PerCall
每次调用:一个新的实例创建的时候,后续又有创建实例的请求来的时候,会先创建完这个实例再去处理后面的请求。
  • Per Session: an instance for each session.WCF Per Session
每个会话:一个实例对应一个会话。
  • Singleton: All incoming requests are served by only one instance.【单例:一个实例,服务所有的请求。】
  • WCF Singleton mode

For details on WCF Instance Management, please refer other article “3 techniques for Instance Management in WCF”.【更多WCF实例管理细节,请参考 “3 techniques for Instance Management in WCF”】

What are the different ways to handle concurrency in WCF?【在WCF中,处理并发性都有哪些方式?】

There are three different ways to handle concurrency in WCF that are:【在WCF中有三种不同的方式,处理并发:】

  • Single【单个】
  • Multiple【多个】
  • Reentrant【可重入】

Single: means at a given time, only a single request can be processed by WCF service instance. Other requests will be waiting until the first one is fully served.

单个:意味着,在给定的时间内,WCF服务实例只能处理单个的请求,其他的请求,将会等到这第一个完全服务完为止。
Multiple: means multiple requests can be served by multiple threads of a single WCF service instance.

多个:意味着一个WCF服务实例的多个线程可以处理多个请求。
Reentrant: means a single WCF service instance can process one request at a given time but the thread can exit the service to call another service.

可重入:意味着,在给定的时间内,一个WCF实例,可以处理一个请求,但是这个线程退出之后,这个服务,又去调用另外的服务。
We can apply these concurrency settings by putting ConcurrencyMode property in ServiceBehavior as follows:

如下面所示:我们可以在ServiceBehavior 中设置ConcurrencyMode 属性,来应用并发性的设置。

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple] public class MyService : IMyService
{
}

 

What is WCF throttling?【什么是WCF节流?】

WCF throttling enables us to regulate the maximum number of WCF instances, concurrent calls and concurrent sessions. Basic purpose is to control our WCF service performance by using Service throttling behavior.

In configuration file we can set this behavior as follows:

WCF节流,能够保证我们去调节WCF最大的实例数量,并发调用和并发会话。基本的目的是,通过使用服务节流的行为,来控制WCF服务性能。

   <serviceBehavior>
        <behavior name=”MyServiceBehavior”>
                    <serviceThrottling
                                     maxConcurrentInstances=”2147483647”
                                     maxConcurrentCalls=”16″
                                     maxConcurrentSessions=”10″
         </behavior>
   </serviceBehavior>

Above given values are the default ones, but we can update it after looking into the requirements of our application.

上面给出的值是默认的,但是我们可以根据我们应用程序的需要更新里面的值。