Javascript asynchronously calls the WCF Service

Source: Internet
Author: User

In the previous blog, I specifically introduced how to call the WCF Service in JavaScript,

However, some specific details are not explained in detail,

Therefore, this blog post will provide some detailed explanations of these details,

A demo is different from the demo in the previous blog,

This demo exposes the interface to JavaScript,

Then let another class implement this interface,

This class then implements the method exposed to Javascript in the interface,

In this way, this method in this class is indirectly exposed to JavaScript.

After talking about so many methods in a class to Javascript,

To ensure that javascript can access methods in the WCF Service,

So how can we publish a method to JavaScript?

First, you must expose it to the class or interface of the JavaScript method,

Apply servicecontractattribute,

Then, you must expose the method to JavaScript.

Application operationcontractattribute,

In fact, in addition to the above, make sure that javascript can access the WCF Service,

You must also perform some necessary configuration in Web. config,

Of course, you can create a new "Ajax-enabled WCF Service"

To automatically complete the configuration in Web. config, although this can be done,

However, let's take a look at the configuration in Web. config,

When I use the "Ajax-enabled WCF Service"

When you create a WCF Service wcfdemo22use. SVC,

Some of the following settings will be added to Web. config (note the comments and tags ),






enablewebscript />





<! -- Note that the contract attribute here is an interface name, not a class name.
This is because servicecontranctattribute is marked in this interface.
Operationcontractattribute is marked in the interface method.
Therefore, the contract used here is the interface name iwcfsample instead of the class name wcfdemo22use.
In the demo of the previous blog post, the class is directly used to add servicecontractattribute.
So the previous blog post corresponds to
Contract = "simple introduction _ ASP. net_ajax.wcfdemo21use" -->

<Endpoint address = ""
Behaviorconfiguration = "in-depth introduction _ ASP. net_ajax.wcfdemo22useaspnetajaxbehavior"
Binding = "webhttpbinding"Contract= "Simple introduction _ ASP. net_ajax.Iwcfsample"/>
</Service>
</Services>
</System. servicemodel>

Use the wcfdemo22use. svc wcf Service created above,

I completed the following demo,

Because it is to understand some of the contentCodeSome are quite simple,

. SVC. CS

Using system;
Using system. servicemodel. activation;

Namespace: simple introduction _ ASP. net_ajax
{
[System. servicemodel. servicecontract (namespace = "baobeime ",
Name = "wcfdemo22use")]
Public interface iwcfsample
{
[System. servicemodel. operationcontract]
String getdate ();
}
[Aspnetcompatibilityrequirements (requirementsmode =
Aspnetcompatibilityrequirementsmode. Allowed)]
Public class wcfdemo22use: iwcfsample
{
// Implement the methods in the iwcfsample Interface
// Because the interface has marked [system. servicemodel. servicecontract]
// The method in the interface is marked with [system. servicemodel. operationcontract]
// This class wcfdemo22use implements this interface again
// If this class implements the methods in the interface
// You do not need to mark the attribute above.
// This method has been made public to Javascript in the interface.
Public String getdate ()
{
Return datetime. Now. tostring ();
}
}
}

. Aspx

<% @ Page Language = "C #" autoeventwireup = "true"
Codebehind = "demo _ 22. aspx. cs"
Inherits = "simple introduction _ ASP. net_ajax.demo _ 22" %>











<Script language = "JavaScript" type = "text/JavaScript">
Function btnone_onclick (){
VaR wcfproxy = new baobeime. wcfdemo22use ();
Wcfproxy. getdate (onsucceededcallback, onfailedcallback );
}

Function onsucceededcallback (result ){
Alert (result );
}

Function onfailedcallback (error ){
Alert (error. get_message ());
}
</SCRIPT>

<Div>
<Input id = "btnone" type = "button" value = "Call the WCF Service"
Onclick = "Return btnone_onclick ()"/>
</Div>
</Form>
</Body>
</Html>

Let's take a look.

Then, we need to parse the code in. SVC,

Because my servicecontractattribute is applied to the interface iwcfsample,

So, on the client, IBaobeime. wcfdemo22use

Yes here

Defined namespace and name,

The demo above uses an interface to implement servicecontractattribute,

If you directly use servicecontractattribute on top of a class,

If namespace and name are not defined,

On the client side, the class name is directly used to initialize a javascript proxy class,

For example, in the demo of the previous blog post, if namespace and name are not set,

You can directly write

However, the namespace and name must be set for this demo,

Because the unique definition in this. SVC must generate the namespace and name of the proxy class on the client,

If namespace and name are not set,

SoProgramNo way to know which namespace is currently exposed to JavaScript.

Pay attention to the above in many places,

If your application fails to be executed, the following three problems may occur:

First, check whether the contract in Web. config is set correctly.

Second, does servicecontractattribute apply correctly?

Third, whether operationcontractattribute is correctly applied

2010-2-03

 

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.