Creation and publication of WCF services to IIS

Source: Internet
Author: User

One, the creation of a WCF service

There are two ways to create them:

1.WCF Service Library

2.WCF Service Application

As shown in the following:

Here you select the WCF service library. Precautions:

The 1.WCF service Library is a class library project, where the. NET 3.5 version is selected (the version can have some conflicts).

2. Because it is a class library, the configuration file is app. Config, and you must rename it to Web. config If you want to publish to IIS. Use the original IService interface and implement the service paradigm. Then, in the Add Web. config configuration:

File name: Web. config:

<?xml version= "1.0"?><configuration>  <system.serviceModel>    <behaviors>      < servicebehaviors>        <behavior name= "ServiceBehavior" >          <servicemetadata httpgetenabled= "true"/ >        </behavior>      </serviceBehaviors>    </behaviors>    < serviceHostingEnvironment multiplesitebindingsenabled= "true"/>    <services>      <service behaviorconfiguration= "ServiceBehavior"               name= "Wcfservicelibrary1.service1" >        <endpoint binding= " Wshttpbinding "                  bindingconfiguration=" "                  contract=" Wcfservicelibrary1.iservice1 "/>      </ service>    </services>  </system.serviceModel></configuration>

In particular, it is important to add:

<servicehostingenvironment multiplesitebindingsenabled= "true"/>

Otherwise, you may

Third, the service cannot be found when you add a reference to the WCF service from the IIS host Reference , and the following error message appears:

An error occurred while downloading "http://127.0.0.1:7293/WCFServerRefWCFLib.svc".
The request failed because of HTTP status 404: Not Found.
The metadata contains a reference that cannot be resolved: "Http://127.0.0.1:7293/WCFServerRefWCFLib.svc".
No endpoint listens for http://127.0.0.1:7293/WCFServerRefWCFLib.svc that can accept messages. This is usually caused by an incorrect address or SOAP operation. If this condition exists, see innerexception for more information.
The remote server returned an error: (404) Not found.
If the service is already defined in the current solution, try to build the solution, and then add the service reference again.

3. Add the Svc file.
Select Add class to find the file template for the SVC type. You can enter "file name +.svc", for example Wcfserverusewcf.svc, and click OK to create a file in SVC format. Clears the contents of the auto-generated wcfserverusewcf.svc. Add the following code:

File name: wcfserverusewcf.svc

<%@ ServiceHost language= "C #" service= "Wcfservicelibrary1.service1"%>

Another way to do this is not to have IIS access the WCF Class Library project directly, but to create a new Web site by:

In the solution node--right--new Web site--Select the WCF service in the popup box. Such as:

Then add the application to the WCF class library. The DLL that references the WCF class Library is seen in the bin after the reference is complete, as shown in:

Then you add the Web. config and. svc files and content, exactly as you did above.

Second, IIS as the host, publish the WCF service

Make sure the iis! is installed

Step 1:

"My Computer" right--"manage"--"services and Applications"--"Interner Information Services (IIS) Manager"--"web site" right-click to add a website. Such as:

Step 2:

Start the site and

Explore Http://127.0.0.1:7293/WCFServerUseWCFLib.svc

If the publication is successful, the following screen will appear in the browser:

Click: HTTP://127.0.0.1:7293/WCFSERVERUSEWCFLIB.SVC?WSDL, will have the following screen in the browser:

Iii. referencing a WCF service from the IIS host

Step 1:

Create a project, where you select a WPF application.

Step 2:

Right-click on "WPF Application"-Add service Reference-address, enter: http://127.0.0.1:7293/wcfserverusewcflib.svc--Click the "Go" button, Discover the WCF service. Rename the service namespace (named here: Wcfserverusewcflib).

Configure as shown:

Click "OK" button;

After the reference succeeds, the app. Config file automatically adds the WCF client side configuration as follows:

File name: App. Config

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.serviceModel> <bindings>                    <wsHttpBinding> <binding name= "Wshttpbinding_iservice1" closetimeout= "00:01:00" opentimeout= "00:01:00" receivetimeout= "00:10:00" sendtimeout= "00:01:00" bypassproxyonlocal= "False" transactionflow= "false" hostnamecomparisonmode= "StrongWildcard" maxbufferpoolsize= "524288" MaxR                    Eceivedmessagesize= "65536" messageencoding= "Text" textencoding= "Utf-8" usedefaultwebproxy= "true" Allowcookies= "false" > <readerquotas maxdepth= "maxstringcontentlength=" 8192 "MaxA" Rraylength= "16384" maxbytesperread= "4096" maxnametablecharcount= "16384"/> &L                    T;reliablesession ordered= "true" inactivitytimeout= "00:10:00" enabled= "false"/> <security mode= "MEssage "> <transport clientcredentialtype=" Windows "proxycredentialtype=" None " Realm= ""/> <message clientcredentialtype= "Windows" negotiateservicecredential= "t Rue "algorithmsuite=" Default "/> </security> </bi nding> <binding name= "Wshttpbinding_iservice11" closetimeout= "00:01:00" Opentimeou t= "00:01:00" receivetimeout= "00:10:00" sendtimeout= "00:01:00" bypassproxyonlocal= "false" Transactionflo W= "false" hostnamecomparisonmode= "StrongWildcard" maxbufferpoolsize= "524288" maxreceivedmessagesize= "65 536 "messageencoding=" Text "textencoding=" Utf-8 "usedefaultwebproxy=" true "Allowcook                        Ies= "false" > <readerquotas maxdepth= "maxstringcontentlength=" 8192 "maxarraylength=" 16384 " MaxbYtesperread= "4096" maxnametablecharcount= "16384"/> <reliablesession ordered= "true" InactivityTim                        eout= "00:10:00" enabled= "false"/> <security mode= "Message" > <transport clientcredentialtype= "Windows" proxycredentialtype= "None" realm= ""/                            > <message clientcredentialtype= "Windows" negotiateservicecredential= "true" algorithmsuite= "Default"/> </security> </binding> & lt;/wshttpbinding> </bindings> <client> <endpoint address= "http://127.0.0.1:729                3/wcfserverusewcflib.svc "binding=" Wshttpbinding "bindingconfiguration=" Wshttpbinding_iservice1 "                    contract= "Wcfserverusewcflib.iservice1" name= "Wcfserverusewcflibiservice" > <identity> <servicepRincipalname value= "host/weime-pc"/> </identity> </endpoint> &LT;ENDP Oint address= "Http://127.0.0.1:7294/WCFServerRefWCFLib.svc" binding= "Wshttpbinding" bindingconfiguration= "W                Shttpbinding_iservice11 "contract=" Wcfserverrefwcflib.iservice1 "name=" Wcfserverrefwcflibiservice "> <identity> <serviceprincipalname value= "host/weime-pc"/> </id entity> </endpoint> </client> </system.serviceModel></configuration>

Step 3:

Import namespaces:

Using Wpfapplication1.wcfserverusewcflib;

Call the WCF service (call the method here :

string GetData (int)

        private void Btnwcflibtoiis_onclick (object sender, RoutedEventArgs e)        {            using ( Wcfserverusewcflib.service1client proxy = new Wcfserverusewcflib.service1client ())            {                MessageBox.Show (this. BtnWCFWebRefWCFLibTOIIS.Content.ToString () + Proxy. GetData (2));            }        }

If you want the client to use asynchronous programming, you can have the Client Agent support asynchronous programming by:

1. Right-click the reference for the WCF service---Select Configure service Reference.

2. In the popup dialog box, tick "generate asynchronous Operation"--click "OK" button.

Iv. Solutions

"The End"

Http://www.cnblogs.com/easy5weikai/p/3341147.html

Creation and publication of WCF services to IIS

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.