Early release of the 4/25 Shanghai club activity speech "centralized WCF Service Configuration Management" example Program

Source: Internet
Author: User
Tags svn client connectionstrings
Document directory
  • SimpleServiceProvider Program
  • SimpleServiceConsumer Program

Thanks to the invitation from the 4/25 Shanghai club activity organizing team, I have the honor to share with you some explorations in "centralized WCF Service Configuration Management" during this activity. In order to give you a more intuitive understanding of the topics I talked about on the day of the activity, I will introduce the sample programs that I will demonstrate in advance.

This example demonstrates the following content:

1) centralized WCF Service configuration management based on SQL Server databases;

2) centralized ConnectingString management;

3) centralized AppVariable management;

4) WCF ServiceHostFactory and WCF ServiceLocator based on centralized configuration management;

5) centralized configuration management-based cross-wcf linq Style paging, sorting query, and DataSource control usage;

6) integrate local ServiceLocator's WCF Service Release and consumption;

(Please use the SVN client to download ):

Http://nintegrate.googlecode.com/svn/trunk/

Installation instructions:

Use the SVN client to download all the code, initialize the configuration database based on readme.txt in the samples \ setupdirectory, and open the solution of the example. To run this example, you must have at least IIS 6, VS2008, and SQL Server 2005 or 2008 or Express installed. IIS7 is required if you want to run the Net. Tcp Binding demo.

Example project:

This example consists of the following four C # project projects:

1. SimpleServiceContracts-ServiceContracts and DataContracts are defined

2. SimpleServiceImpls-contains the ServiceContracts implementation class

3. SimpleServiceProvider-service publishing Web programs

4. SimpleServiceConsumer-service consumption Web program

The four sample projects depend on NIntegrate and NIntegrate respectively. query, NIntegrate. query. command and NIntegrate. the four Web projects are composed of the NIntegrate-based SOA framework based on the WCF and centralized configuration (for more information about this framework, the blog of Teddy will introduce more in the future, here let's focus on the sample program itself ).

SimpleServiceProvider Program

This program uses IIS to publish two WCF services: QueryService. svc and SimpleService. svc. The content of these two. svc files is as follows:

QueryService. svc-NIntegrate query service, implemented by NIntegrate

1 <% @ ServiceHost Service = "NIntegrate. query. IQueryService, NIntegrate. query, Version = 0.5.0.1, Culture = neutral, PublicKeyToken = e2b9e2165dbdd5e6 "Factory =" NIntegrate. wcfServiceHostFactory, NIntegrate, Version = 0.5.0.1, Culture = neutral, PublicKeyToken = e2b9e2165dbdd5e6 "%>

 

SimpleService. svc-a simple SayHello service, used to demonstrate centralized configuration of multiple Binding, multi-Endpoint WCF Service Release and consumption

1 <% @ ServiceHost Service = "SimpleServiceContracts. ISimpleServiceDemo, SimpleServiceContracts" Factory = "NIntegrate. WcfServiceHostFactory, NIntegrate, Version = 0.5.0.1, Culture = neutral, PublicKeyToken = token" %>

There are two points worth noting:

1. The services of these two services specify the ServiceContract interface type of the Service rather than the specific implementation class of the Service.

2. The two services automatically create the corresponding ServiceHost through the specified ServiceContract interface type and SQL Server-based centralized configuration database through the WcfServiceHostFactory implemented by NIntegrate, and publish the service.

 

Resolution:

How does WcfServiceHostFactory internally find the specific implementation class through the specified ServiceContract interface type? It is implemented through a local ServiceLocator that can be configured in Web. config. NIntegrate has a built-in implementation of AppVariableServiceLocator Based on the centralized configuration of the SQL Server database, and provides convenient interfaces with third-party ServiceLocator, such as Unity, the IoC containers such as Castle are integrated to automatically locate the implementation of interfaces. In this example, the service implementation class corresponding to the ServiceContract interface type is mapped through the AppVariable table in the database and located using the AppVariableServiceLocator class.

 

In the Web. config of SimpleServiceProvider, you only need to set an AppCode and a ConnectionString, without any WCF configuration information:

1...
2 <deleetask>
3 <add key = "NIntegrate. Configuration. AppCode" value = "SP_SSP"/>
4 <! -- To set service locator wrapper for external IoC container here, by default, the build-in AppVariableServiceLocator is used. -->
5 <! -- <Add key = "NIntegrate. ExternalServiceLocator" value = "NIntegrate. AppVariableServiceLocator, NIntegrate, Version = 0.5.0.1, Culture = neutral, PublicKeyToken = Taobao"/> -->
6 </appSettings>
7 <connectionStrings>
8 <add name = "NIntegrate. configuration "connectionString =" Data Source = vista; Initial Catalog = NIntegrateDemo; Persist Security Info = True; User ID = nbear; Password = nbear "providerName =" System. data. sqlClient "/>
9 </connectionStrings>
10...

 

SimpleServiceConsumer Program

This program is also a Web program deployed in IIS. It contains only one Default. aspx page and one Web. config file.

His Web. config is the same as the Web. config of SimpleServiceProvider. You only need to set an AppCode and a ConnectringString:

1...
2 <deleetask>
3 <add key = "NIntegrate. Configuration. AppCode" value = "SP_SSC"/>
4 </appSettings>
5 <connectionStrings>
6 <add name = "NIntegrate. Configuration" connectionString = "Data Source = vista; Initial Catalog = NIntegrateDemo; Persist Security Info = True; User ID = nbear; Password = nbear"
7 providerName = "System. Data. SqlClient"/>
8 </connectionStrings>
9...

 

The Default. aspx page demonstrates the use of QueryService. svc and SimpleService. svc. The interface for running the Default. aspx page is as follows:

You can see from the interface that there are several GridView nodes that can sort and edit the Binding and Endpoint of QueryService and SimpleService. They are implemented by using the QueryDataSource control provided by NIntegrate Based on QueryService. svc. The last line is the output of SimpleService. svc call and the currently used WCF Binding type.

Currently, both QueryService and SimpleService run the "Sample-Shared Binding" Binding configured in the database. We can directly modify the Binding type and configuration information in the GridView. After Binding is modified, You need to recycle the AppPool in IIS to make the new configuration take effect. (Note that only IIS7 supports modification to a non-HTTP Binding, and you must enable the corresponding net. tcp or net. pipe Binding support in IIS ).

SimpleService has the second NetTcpBinding named "SimpleServiceDemo Binding 2". If you are using IIS7 and have enabled net. tcp Binding in IIS, click Enable to activate it. If you run Default. aspx again in the AppPool in recycle IIS, you will see that the Binding Type in the last line is changed to "NetTcpBinding ". That is to say, if a Service has different types of endpoints in multiple activation states, the ServiceManager provided by NIntegrate ensures that the optimal Binding is used. For example, SimpleService has the Binding WSHttp and NetTcp. Of course, NetTcp is the best. Further. If local ServiceLocator is configured and the corresponding implementation class is specified for SimpleService, you can also guess that the "better" local Service implementation will be used, instead of using the WCF channel.

 

How does the QueryDataSource control display, sort, and modify configuration information such as Service, Binding, and Endpoint through QueryService. svc? In fact, the query syntax provided by QueryService not only supports Simple CRUD, but also supports the query language of LINQ Style, a variety of database support, database-specific function extension and paging sorting, and the query language can be compatible with cross-WCF and COM (that is, it can be used in non.. NET applications, such as ASP, use the same LINQ Style query language to remotely access the services released by WCF for the same paging sorting query ). Here, I will sell a customs clearance, so I will not introduce it much. You can choose to read the code by yourself (including the NIntegrate core class library and the example are all open source code based on the BSD Protocol ), join in club activities and discuss with Teddy or wait for Teddy's blog to discuss special topics in the future.

Now let's introduce this much. If you have any comments or suggestions, please contact Teddy through various channels. If you join the 4/25 event, you are welcome. However, I know that many of my friends are planning to go to the Shanghai Auto show this weekend. It doesn't matter. I am also satisfied with this post.

// I am the ending character

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.