In-depth integration of WCF and ASP. NET web applications

Source: Internet
Author: User

In development practice, a common situation is: first develop an Asp.net web application in a typical way, and then for some reasons (for example, hope to add more access methods to the client through the mobile phone). Of course, the server-side solution is of course the preferred choice for WCF.

However, if you integrate WCF with the existing web application, you need to handle it with caution. for the same operation, the performance may be reduced by dozens of times or even more than the internal call of the original application in the normal Web service mode, however, we do not want to see this dramatic decline in performance.

To provide the service access interface without degrading the existing system performance or damaging the original system architecture, I adopted the following solution:

1. embed the WCF Service into the web application project.

If you choose to embed a WCF Service into a web application (instead of creating another project), you have two objectives:

(1) the WCF Service can directly call the functions in the original webapp without the need to copy the code or destroy the original architecture. copying code may cause a series of adverse consequences. This will not be redundant. Changing the original webapp architecture will lead to high costs or further performance degradation.

(2) webapps no longer need to use any channel to call the WCF function. They can directly create an instance of a service class as they use a common class and call its method. this is important. in fact, the main user of the service is webapp, or if you do not consider adding a client type, you do not need to introduce WCF. These functions are part of webapp. if these functions are completely independent from the webapp, it will inevitably lead to some channel for communication when the webapp accesses the service, which will inevitably lead to a certain performance decline, for some features with high frequencies, this may bring obvious response speed barriers. by embedding the WCF Service into a webapp project, the performance impact on webapp itself caused by the introduction of WCF is completely eliminated.

Operation Method:

Directly Add a new item in vs-> WCF Serivce.

In addition. add the corresponding service configuration in config: Add <system. servicemodel> node (if you already have... then you don't need to add ...), the configuration is as follows:

<System. servicemodel>
<Services>
<Service name = "Goodbaby. testservice">
</Service>
</Services>
</System. servicemodel>

The service name depends on the namespace and service class name of the actual project.

2. Open the Named Pipes channel so that other applications hosted on the same server can access the WCF Service through named pipes.

Named Pipes channel has the highest performance among various communication methods of WCF, but it can only be called by another application on the same host. if only this application is deployed on the server, you can ignore this section. however, if other applications are deployed on the server and they need to access the webapp, Using Named Pipes is the best choice.

In WCF, the Named Pipes channel is implemented through netnamedpipebinding. Therefore, you only need to configure an endpoint using the binding.

Operation Method:

Add the corresponding endpoint under the service node of Web. config. The configuration content is as follows:

<Service name = "Goodbaby. testservice">
<Endpoint address = ""
Binding = "netnamedpipebinding"
Contract = "Goodbaby. itestservice">
</Endpoint>
<Host>
<Baseaddresses>
<Add baseaddress = "net. Pipe: // localhost/webapp/WebService/testservice. SVC"/>
</Baseaddresses>
</Host>
</Service>

Here, an endpoint node and a host node are added under the service added above. the address is not specified in the endpoint, indicating that it will use baseaddress. Binding is set to the built-in netnamedpipebinding, and contract is the interface name used by the WCF Service.

In baseaddress, net. pipe indicates that this is a Named Pipes channel. Since this channel can only be called on the local machine, its host address should always be localhost, and webapp is the actual application name, webService is a folder name in the project, while testservice. SVC is the file name of the WCF Service.

Enable named pipes (net. Pipe Binding) support on IIS

Originally, you can test the service after completing the configuration in Web. config, but by default, IIS does not support net. Pipe Binding. Therefore, you need to perform the following steps to enable the Service.

(1) Open Control Panel-> turn windows features on or off-> Microsoft. net Framework 3.5.1 (this version depends on the version actually installed), check windows Communication Foundation non-HTTP activation, and save.

(2) Select the default web site (or other websites added by yourself) of IIS, and click bindings (such as :) on the actions Panel on the right :).

In the pop-up panel, check whether the net. Pipe Binding has been added:

(3) Expand default web site, click to select a specific web application name, and click Advanced settings in the actions Panel on the right, as shown in:

On the settings panel that appears, check whether the enabled protocols contains net. Pipe.

(4) reset IIS.

If you still cannot browse the service after completing the preceding settings, you may need to reset IIS.

Open a cmd command window and enter the following command:

> CD \ windows \ microsoft.net \ framework \ v4.0 *

The last directory is the version number. Please refer to the actual installation version number.

Then execute aspnet_regiis-I

After the execution is complete, execute iisreset

(5) Browse services in IIS.

In IIS content view, select the WCF Service file (testservice. SVC), right-click, and choose browse. The default service browsing interface is displayed:

You have created a service.

To test this service, you will need to create a client and use it to call the service ......

3. for LAN users, use TCP channels as much as possible to access the WCF Service..

The performance of the TCP channel is about 10 times faster than that of normal HTTP. To enable the TCP channel on the server side is very similar to that of the above named pipes, add an endpoint, enable the corresponding support on IIS. The specific method is very similar to Section 2nd and will not be described in detail.

4. The HTTP channel should also be retained.

Although the typical HTTP channel (that is, the Web service in the general sense) has poor performance, its ability to pass through the firewall and Its versatility are incomparable to those of other channels, when none of the current channels are available, the HTTP channel should be used to communicate with the WCF Service.

The specific implementation method is similar to other binding methods, and is also the most mentioned in various materials.

Summary

With the help of WCF's integration of communication methods, when multiple access channels are implemented, the actual modification to the project is only on the web. add an endpoint in config (and the corresponding binding configuration, if needed), so that no matter how you access the WCF Service, or even not treat it as a service, instead, it is directly viewed as a class of the project, and the same code is executed. This avoids copying the same function several times and storing it in different projects, it also ensures optimal performance in various contexts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.