Tutorial-how to configure net. TCP communication in Silverlight!

Source: Internet
Author: User
Preface

A recent SL project, because SL itself is based on.. NET Framework, but independent. net Framework kernel, which has many limitations. net Framework is so powerful. For example, the ape who often develop winform must be uncomfortable. The familiar datatable is gone, and the familiar iasyncresult interface is gone ~. Therefore, in the SL environment, WCF communication becomes very important. Note: You may already have access to the WCF Ria service, but we will not discuss it this time. Most of them are based on SQL SERVER + EF4. If you are interested, you can go to the garden to find related blog posts.

Since WCF is a big topic, and this article does not explain WCF, you need to have a certain level of WCF foundation when reading this article, if you don't have one, read the articles in the artech's WCF series. Note that only sl4 and later versions support the net. TCP protocol, so pay attention to your current SL version.

Create a project

Open your vs2010

1. Create a blank solution

2. Create a WCF Service Application Service

3. Create an SL client. Do not select enable Ria service when creating the client.

4. You can build your solution after you build it.

For example

Configuration Service

To directly describe how to configure the service, we will not re-design the service here. We will use the project to create the default iservice1, so let's talk about how to configure the service.

1. First open some of your functional services, I use win7, so you need to find the following options in the control panel-> Program-> enable or disable the WINDOWS function

Click "OK". After the selection, you will find the following started services in the service,

2. After the above configuration is completed, the next step is to modify the IIS on the server side in the solution, and change the IIS In the vs field to the local IIS.

Right-click slnettcpdemo. Server-> properties-> Web-> User Local IIS Web Server

Remember to click "create virtual directory" to create a virtual directory under your local IIS default website.

After adding the virtual directory, configure the local IIS to support net. TCP communication.

Open IIS, find the default site of local IIS, and select Edit binding.

After completing the preceding steps, we need to set your virtual directory to net. TCP support. Therefore, select the created virtual directory, edit the advanced settings of the virtual directory, and add the net. TCP support. For example

In the end, we will place the following clientaccesspolicy file under the default site. XML, which is a configuration policy file that tells IIS how to process. net. for TCP protocol, remember that this file must be named like this. Do not rename it, as defined in ms.

Next, go back to the slnettcpdemo. server project and edit the Web. config file, for example

<?xml version="1.0"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web>  <system.serviceModel>    <services>      <service name="SLNettcpDemo.Server.Service1" behaviorConfiguration="behavior1">        <endpoint address="Service1" contract="SLNettcpDemo.Server.IService1" binding="netTcpBinding" bindingConfiguration="tcpConfig" />        <endpoint contract="IMetadataExchange" binding="mexTcpBinding" address="mex" />        

 

After the configuration is complete, we need to verify whether our service can be used normally. Right-click the service1.svc file and choose browse in the browser. When you see the following, your server configuration is complete. Congratulations!

Configure the client

When the server configuration is complete, it is much easier to configure the client. Right-click the project slnettcpdemo. slclient and add web service reference. For example, pay attention to the reference address.

After you click OK, the Directory of the slnettcpdemo. slclient project will change, for example:

The clientconfig file is very important. Let's see what it contains.

<configuration>    <system.serviceModel>        <bindings>            <customBinding>                <binding name="NetTcpBinding_IService1">                    <binaryMessageEncoding />                    <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />                </binding>            </customBinding>        </bindings>        <client>            <endpoint address="net.tcp://localhost:4502/SLNettcpDemo.Server/Service1.svc/Service1"                binding="customBinding" bindingConfiguration="NetTcpBinding_IService1"                contract="Service1.IService1" name="NetTcpBinding_IService1" />        </client>    </system.serviceModel></configuration>

 

At this time, we will perform some simple encoding and open mainpage. XAML. CS for editing, as shown below:

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using SLNettcpDemo.SLClient.Service1;namespace SLNettcpDemo.SLClient{    public partial class MainPage : UserControl    {        public MainPage()        {            InitializeComponent();            Loaded += new RoutedEventHandler(MainPage_Loaded);        }        void MainPage_Loaded(object sender, RoutedEventArgs e)        {            var svcClient = new Service1Client();            svcClient.GetDataCompleted += (obj, arg) => {                if (arg != null && arg.Result != null) {                    MessageBox.Show(arg.Result);                }            };            svcClient.GetDataAsync(123);        }    }}

 

After editing, set F5 to complete.

 

Notes

  1. the thread pool corresponding to the local default site must be.. NET Framework 4, so when your site is not supported. NET framework 4, please register with IIS again. NET framework 4, how to register a website for collection, a lot of tutorials.

2. During deployment, both the client configuration and the server configuration must replace localhost with the actual address. Therefore, the insurance method is to replace the entire solution in.

3. All service reference operations on the client are asynchronous, so pay attention to it.

4. This method is easy to familiarize yourself with future deployment, but it is very inconvenient to debug the server code, almost zero. Therefore, there are some solutions:

A. Write the server unit test manually.

B. Here is a post about console programs. You can check it out if you are interested.

5. Because nettcp requires iis7 support, the development environment is a friend of XP/win2003. It seems very troublesome to say that we recommend that you do not bother with it.

6.net. TCP supports very few ports on IIS. It seems to be between-. Please pay attention to this.

Summary

Nettcp is not suitable for applications on the Internet. It is extremely suitable for LAN development, especially enterprise-level development. sl4 supports nettcp, the data access efficiency of SL is greatly improved compared with the original HTTP protocol. At the same time, it also has good support for the WCF communication duplex. Therefore, if you are interested, you may wish to have fun.

In the end, please allow me to boast with a thick face. If you think this article is useful, please click here. Thank you (* ^__ ^ *)......

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.