wcf--two solutions referencing a WCF service

Source: Internet
Author: User
Tags soap readline

Background: I said before that I took part in a BS project, and from the very beginning there was a purpose, and how the two solutions were called each other. From the initial curiosity, to the inquiry, to the inquiry, to the last of their own point of primary exploration. Share the results of your own exploration below. creation and startup of WCF services

First step: Create a service

   [ServiceContract]                                                                                                          public interface Ihello            //The interface that provides the remote service
    {
        [OperationContract]               
        string SayHello (string name);
        [OperationContract]
        int sayint (int a,int b);
    }


 public class Hello:ihello     //Implement service interface
    {public
        string SayHello (string name)
        {
            return name + "Hello." ";
            throw new NotImplementedException (); The interface is not implemented.
        }


        public int sayint (int a,int b)
        {
            return a + b;
        }
    }

Step two, configure the endpoint, start the service

 static void Main (string[] args) {//nettcpbinding mode starts WCF service ServiceHost m_servicehost = NE      W ServiceHost (typeof (Hello));//service1 is the class name of the WCF service basichttpbinding binding = new BasicHttpBinding (); Transport protocol URI baseaddress = new Uri (string.   Format ("Http://localhost:8000/HelloServiceDemo2"));  Service Base Address M_servicehost.addserviceendpoint (typeof (Helloservicedemo2.ihello), binding, baseaddress);                                                                                  Configure endpoint//basichttpbinding mode to start a WCF service
            The following code does not understand, the great god many advice servicemetadatabehavior metadatabehavior;
            Metadatabehavior = m_servicehost.description.behaviors.find<servicemetadatabehavior> ();
                if (Metadatabehavior = = null) {Metadatabehavior = new ServiceMetadataBehavior ();
                Metadatabehavior.httpgetenabled = true; Metadatabehavior.httpgeturl = new Uri (strIng.
                Format ("Http://localhost:8000/HelloServiceDemo2"));
            M_SERVICEHOST.DESCRIPTION.BEHAVIORS.ADD (Metadatabehavior);    } m_servicehost.open ();
            Start the service Console.WriteLine ("Service has been started");
        Console.ReadLine (); }

Client Reference Service The first step is to create a solution to download the WCF service. Step: After creating the good one solution, then right-click the reference, select Add Service Reference, go to the following interface, fill in the base address, you can download the service. Click OK.


The second step, the method of invoking the service

public partial class WebForm1:System.Web.UI.Page
    {
        protected void Page_Load (object sender, EventArgs e)
        { C4/>webapplication2. servicereference2.helloclient Hello = new WebApplication2. ServiceReference2. Helloclient ();      Note here that helloclient is under the namespace of the service name under the solution.
            string a = Hello.sayhello ("Wugangpeng");
            Response.Write (a);

            int sum = Hello.sayint (4, 2);
            Response.Write (sum);     
        }
    }

I started with a problem . I wrote the endpoint in the config file and started the service.

    Contents of the configuration file                                                                                                         <system.serviceModel>
		<services>
			<service name= "Myhost.hello" >
				< host>
					<baseAddresses>
						<add baseaddress= "Http://localhost:8000/HelloServiceDemo2"/>             
					</baseAddresses>
				
ServiceHost host = new ServiceHost (typeof (Hello  ));   The host program starts the service
            host. Open ();
            Console.WriteLine ("Service has been started");
            Console.ReadLine ();
Then the download service is out of the question. An error occurred while downloading "http://localhost:8000/HelloServiceDemo2/_vti_bin/ListData.svc/$metadata".
The request failed with an HTTP status of 400:bad request.
The metadata contains a reference that cannot be resolved: "Http://localhost:8000/HelloServiceDemo2".
The service Http://localhost:8000/HelloServiceDemo2 does not support content type Application/soap+xml; Charset=utf-8. The client and service bindings may not match.
The remote server returned an error: (415) cannot process the message because the content type ' application/soap+xml; Charset=utf-8 ' is not the expected type ' text/xml; Charset=utf-8 '.
If the service is already defined in the current solution, try to build the solution, and then add the service reference again.


To the present, or to the WCF ignorant. There is no clue to these errors at all. Seek the help of the great God.

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.