Use the WCF Service that supports both Ajax Json and. NET clients in ASP. NET 3.5

Source: Internet
Author: User
Recent development requires that you use the IIS-hosted WCF to provide data and implementation operations. The client uses Ajax Json and WinForm. Therefore, you must bind webHttpBinding for Ajax and wsHttpBinding for WinForm at the same time on the unified service.

The service name is Itoro. WebCore. Web.

When creating a service, select "AJAX-enabled WCF Service" and. config will automatically go to system. the relevant configuration is generated under serviceModel. The default configuration for Ajax is Itoro. webCore. webAspNetAjaxBehavior endpoints, and use <enableWebScript/> to enable Ajax Javascript support. In the service configuration, it is a service named Itoro. WebCore. Web. The endpoint is configured to use Itoro. WebCore. WebAspNetAjaxBehavior and bind it to webHttpBanding.

Next, manually modify the configuration to support WinForm client access, that is, wsHttpBinding.

Add the following content under system. serviceModel/behaviors/serviceBehaviors (if serviceBehaviors does not exist, create this element in the corresponding location ):

<Behavior name = "Itoro. WebCore. WebBehavior">
<ServiceMetadata httpGetEnabled = "true"/>
</Behavior>

This configuration specifies the service behavior named Itoro. WebCore. WebBehavior to enable service metadata under Http.

Next, modify the service named Itoro. WebCore. Web (that is, the <service name = "Itoro. WebCore. Web"/> element) as follows:

<Service behaviorConfiguration = "Itoro. WebCore. WebBehavior" name = "Itoro. WebCore. Web">
<Endpoint address = "web" behaviorConfiguration = "Itoro. WebCore. WebAspNetAjaxBehavior"
Binding = "webHttpBinding" contract = "Itoro. WebCore. Web"/>
<Endpoint address = "ws" binding = "wsHttpBinding" contract = "Itoro. WebCore. Web">
<Identity>
<Dns value = "localhost"/>
</Identity>
</Endpoint>
<Endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/>
</Service>

Add the behaviorConfiguration in the service element and add two endpoints, respectively wsHttpBinding for WinForm and mexHttpBinding for metadata. In addition, modify the address attributes of webHttpBinding and wsHttpBinding respectively on the web and ws (which can also be customized, but must be set and cannot be the same, otherwise, an error occurs because different types of bindings listen to the same Uri ).

The complete configuration is as follows:

<System. serviceModel>
<Behaviors>
<EndpointBehaviors>
<Behavior name = "Itoro. WebCore. WebAspNetAjaxBehavior">
<EnableWebScript/>
</Behavior>
</EndpointBehaviors>
<ServiceBehaviors>
<Behavior name = "Itoro. WebCore. WebBehavior">
<ServiceMetadata httpGetEnabled = "true"/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
<ServiceHostingEnvironment aspNetCompatibilityEnabled = "true"/>
<Services>
<Service behaviorConfiguration = "Itoro. WebCore. WebBehavior" name = "Itoro. WebCore. Web">
<Endpoint address = "web" behaviorConfiguration = "Itoro. WebCore. WebAspNetAjaxBehavior"
Binding = "webHttpBinding" contract = "Itoro. WebCore. Web"/>
<Endpoint address = "ws" binding = "wsHttpBinding" contract = "Itoro. WebCore. Web">
<Identity>
<Dns value = "localhost"/>
</Identity>
</Endpoint>
<Endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/>
</Service>
</Services>
</System. serviceModel>

Finally, some changes are required in service usage. Assume that our service is http: // localhost/Web. svc, you should use Web when adding services on the Ajax website. svc/web, and the generated Script Reference address is Web. svc/web/js, where the web is the address in webHttpBinding. In WinForm, you can directly use http: // localhost/Web. add service reference in the generated app. config/configuration/system. serviceModel/client has two endpoints, one of which is WebHttpBinding_Web and the other is WSHttpBinding_Web. Therefore, you must specify the endpoint as WSHttpBinding_Web during use, that is, WebClient client = new WebClient ("WSHttpBinding_Web"); otherwise, an error occurs during running.

Finally, because the Web is stateless and WinForm is stateful, you need to use cookies (including sessions) on the Web ), in WinForm, as long as the WebClient client is not destroyed, the same status will be retained, because the two compatibility modes should also be considered during service writing.

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.