Silverlight + new WCF instance chess WCF communication cross-origin (15th)

Source: Internet
Author: User

In this section, we separate the WCF Service and do not place it in Web applications.

Let's start another project. The name of the previous section is Hellow, And the name is World:

File-> New-> Project-Silverlight application-named: World

After confirmation, the following two projects are available: World and World. web applications.

 

Right-click the solution and choose Create Project: Create a WCF Service Application. Then, enter WorldService:

Next, we will delete the default Service1.cs and Service1.svc:

After deletion, we create a new Service called Service. svc.

Modify the service port in advance. After adding a service reference, you do not need to modify the port in the configuration file.

Now we have another method for the interface, GetWrold:

At the same time, create an object class MyWorld, which is used for returning objects. The above mark on the top of the object class will not be mentioned after the previous section has said.

[ServiceContract]
Public interface IService
{
[OperationContract]
MyWorld GetWorld (int id );
}
[DataContract]
Public class MyWorld
{
[DataMember]
Public int ID
{
Get;
Set;
}
[DataMember]
Public string Name
{
Get;
Set;
}
}

 

Well, the following is a simple interface method:

Public class Service: IService
{
# Region IService Member
Public MyWorld GetWorld (int id)
{
MyWorld world = new MyWorld ();
World. ID = id;
World. Name = "Name is:" + id;
Return world;
}
# Endregion
}

 

OK. the WCF Service method is complete. It's easy.

Now the client is calling. Add service reference as in the previous section:

Change the service name to WorldService. After confirmation, a new folder and configuration file are added.

We do not need to change the port number in the configuration file here, because we have set the port number of the Project attribute in advance.

OK. Then we still need to get the same interface as the previous one to call. Copy The xaml code from the previous section:

 

<Grid x: Name = "LayoutRoot" Background = "White">
<Button Content = "WCF call" Height = "23" HorizontalAlignment = "Left" Margin = "84,111, 0, 0 "Name =" btnCallWCF "verticalignment =" Top "Width =" 75 "Click =" btnCallWCF_Click "/>
<TextBox Height = "23" HorizontalAlignment = "Left" Margin = "120," Name = "txtName" VerticalAlignment = "Top" Width = ""/>
<TextBlock Height = "23" HorizontalAlignment = "Left" Margin = "228,71," Name = "tbMsg" Text = "displayed content" verticalignment = "Top"/>
</Grid>

 

The background Code call is similar to the following:

Private void btnCallWCF_Click (object sender, RoutedEventArgs e)
{
Binding binding = new BasicHttpBinding ();
EndpointAddress endPoint = new EndpointAddress ("http: // localhost: 54321/Service. svc ");
WorldService. ServiceClient client = new WorldService. ServiceClient (binding, endPoint );
Client. GetWorldCompleted + = new EventHandler <WorldService. GetWorldCompletedEventArgs> (client_GetWorldCompleted );
Client. GetWorldAsync (int. Parse (txtName. Text ));
}

Void client_GetWorldCompleted (object sender, WorldService. GetWorldCompletedEventArgs e)
{
WorldService. MyWorld world = e. Result;
If (world! = Null)
{
TbMsg. Text = world. Name;
}
Else
{
TbMsg. Text = "return NULL ";
}

}

 

The previous section illustrates the meaning of each line of code. This section will not be repeated.

However, there is an e. Result parameter that will return different types depending on the return value of your method. Therefore, e. Result is of the MyWorld type, not the string type in the previous section.

Write everything according to the formal code, press F5, and run

Enter number 1: [the server directly uploads a number. Do not write anything else]

Press enter to call. Ah, an exception occurs:

Let's take a look at the cross-origin error.

We use Firefox to trace the call. Let's take a look at it:

No. Two 404 files cannot be found. The official statement is as follows:

In case of Cross-origin, the first configuration file clientaccesspolicy. xml will be found first.

If not, find the second configuration file crossdomain. xml.

Therefore, you can add a new clientaccesspolicy. xml file. The second configuration file does not need to be used, because it will not find the second one after it is found.

Right-click the WCF Service Application of WorldService and add the file-> xml file:

After confirmation, the xml content is:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Access-policy>
<Cross-domain-access>
<Policy>
<Allow-from http-request-headers = "*">
<Domain uri = "*"/>
</Allow-from>
<Grant-to>
<Resource path = "/" include-subpaths = "true"/>
</Grant-to>
</Policy>
</Cross-domain-access>
</Access-policy>

 

 

After saving, run F5 again, or enter 1:

Press enter to call:

Everything is normal. The cross-origin issue is solved by a configuration file.

OK, this section is here. Next we will briefly talk about duplex communication.

Download source code: Click to download

 

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.