简体   繁体   中英

WCF service 404 Not found

I have a basic WCF library project which I am trying to host in console application. The Program.cs file's Main method is as given below:

    static void Main(string[] args)
    {
        // Create a binding and set the security mode to Message.
        BasicHttpBinding b = new BasicHttpBinding();//WSHttpBinding(SecurityMode.Message);

        Type contractType = typeof(SecureWCFLib.IService1);
        Type implementedContract = typeof(SecureWCFLib.Service1);

        Uri baseAddress = new Uri("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/");

        ServiceHost sh = new ServiceHost(implementedContract, baseAddress);

        sh.AddServiceEndpoint(contractType, b, "Service1");

        ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
        sm.HttpGetEnabled = true;
        sh.Description.Behaviors.Add(sm);

        sh.Open();
        Console.WriteLine("Listening");
        Console.ReadLine();
        sh.Close();


    }

I have another console application which acts as a client. I am trying to consume the service in Program.cs as given below:

 static void Main(string[] args)
        {   
            IService1 productChannel = null;
            EndpointAddress productAddress = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/");
            productChannel = ChannelFactory<IService1>.CreateChannel(new BasicHttpBinding(), productAddress);
            string result = productChannel.GetData(123);
            Console.WriteLine(result);
            Console.Read();
        }

But I get exception as

{"The remote server returned an error: (404) Not Found."}

Please let me know what I am doing wrong over here.

If you want have access to your web service on IIS express by other clients , you must grant remote access to IIS express's port. Please see below link .

http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer

http://www.iis.net/learn/extensions/using-iis-express/handling-url-binding-failures-in-iis-express

EndpointAddress productAddress = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/Service1"); 

add Services1 to the end of constructor parameter.

client endpointaddress = serviceshost_baseAddress + relative address.

Your code is equale to configuration:

<service name="SecureWCFLib.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="Service1" binding="basicHttpBinding" contract="SecureWCFLib.IService1">
        </endpoint>
<behaviors>
<!--......-->
<behaviors>
</service>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM