The configurationname attribute that is easily ignored in WCF

Source: Internet
Author: User

When using the WCF Service, svcutil is usually used to generate the proxy class and configuration, and the service can be called with the generated default configuration. Let's take a look at the generated default configuration content:

View code

< Client >
< Endpoint Address = "Http: // localhost: 8732/confignameservice/service1 /"
Binding = "Wshttpbinding" Bindingconfiguration = "Wshttpbinding_iservice1"
Contract = "Iservice1" Name = "Wshttpbinding_iservice1" >
< Identity >
< DNS Value = "Localhost"   />
</ Identity >
</ Endpoint >
</ Client >

Contract refers to the interface or contract class in the proxy class. If contract is defined as follows, It is the name of the Contract interface by default.

View code

1 [System. servicemodel. servicecontract]
2 Public Interface Iservice1
3 {
4 }

However, if multiple services exist or you need to use namespaces to mark contract, you need to modify the name of contract in the endpoint in the client configuration to include the namespace, for example, as follows:

Contract = "confignameservice. iservice1 ".

Generally, the system finds the interface in the proxy class according to the type name, but this is not the case.CodeThe system will prompt that the default endpoint cannot be found.

 

View code

 

1 Using ( VaR Client = New Service1client ())
2 {
3 Console. writeline (client. getdata ( 1 ));
4 }

Change the value of contract back to "iservice1.

The reason is that the configurationname set during the contract definition in the proxy class determines the value to be configured during configuration. This attribute is easily ignored, especially the proxy class generated through svcutil. For example, the following code:

View code

1 [System. servicemodel. servicecontractattribute (configurationname = " Iservice1 " )]
2 Public Interface Iservice1
3 {
4
5 [System. servicemodel. operationcontractattribute (Action = " Http://tempuri.org/IService1/GetData " , Replyaction = " Http://tempuri.org/IService1/GetDataResponse " )]
6 String Getdata ( Int Value );
7
8 [System. servicemodel. operationcontractattribute (Action = " Http://tempuri.org/IService1/GetDataUsingDataContract " , Replyaction = " Http://tempuri.org/IService1/GetDataUsingDataContractResponse " )]
9 Confignameservice. compositetype getdatausingdatacontract (confignameservice. compositetype composite );
10 }

The configurationname is the name of the original service interface. Therefore, you must manually change the configurationname to the required value. Here, you can only change the configurationname defined by the interface in the customer proxy class. Then, the contract value when configuring the endpoint must be consistent with the configurationname value.

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.