开发者

Self hosted WCF Service not working when i type url in browser?

I'm new to wcf. I have Made simple self hosted service and added app.config but when I type address in the browser it is not showing me the service page that we get when we create our service http://localhost:8067/WCFService it is not displaying the service as it shows when we run service. But when i try to add base service in public static void main instead of app.config it works fine I'm not getting?? Can anyone please help me?

Following is the app.config file manually added:

<configuration>
    <system.serviceModel>
        <services>
            <service name="SelfHostedWCFService.WCFService">
                <endpoint 
                 address="http://localhost:8067/WCFService" 
                 binding="wsHttpBinding" 
                 contract="SelfHostedWCFService.IWCFService">
                </endpoint>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Following is the Program.cs:

static void Main(string[] args)
{
    ServiceHost host = new ServiceHost(typeof(SelfHostedWCFService.WCFService));
    host.Open();
    Console.WriteLine("Server is Running...............");
    Console.ReadLine();
}

Following is the Interface file Manually added:

namespace SelfHostedWCFService
{    
    [ServiceContract]
    interface IWCFService
    {
        [OperationContract]
        int Add(int a,int b);

        [OperationContract]
        int Sub(int a,int b);

        [OperationContract]
        int Mul(int a, int b);
  开发者_JAVA百科  }
}

Following is the Service cs file Manually added:

namespace SelfHostedWCFService
{
    class WCFService : IWCFService
    {
        public int Add(int a, int b)
        {
            return (a + b);
        }

        public int Sub(int a, int b)
        {
            return (a-b);
        }

        public int Mul(int a, int b)
        {
            return (a*b);
        }
    }
}

Is something wrong with my app.config or some other concept??


You will need to add the meta endpoint to the self hosted service as well...

ServiceMetadataBehavior meta = new ServiceMetadataBehavior();
      meta.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
      _host.Description.Behaviors.Add(meta);

      _host.AddServiceEndpoint(
        ServiceMetadataBehavior.MexContractName,
        MetadataExchangeBindings.CreateMexHttpBinding(),
        "http://localhost:8067/WCFService/mex"
      );


You should try to import System.ServiceModel.Web to your Interface project, and add [WebGet] attribute for your OperationContract(s)

The interface would look as below:

using System.ServiceModel.Web;
namespace SelfHostedWCFService
{    
        [ServiceContract]
        interface IWCFService
        {
            [OperationContract]
            [WebGet]
            int Add(int a,int b);

            [OperationContract]
            [WebGet]
            int Sub(int a,int b);

            [OperationContract]
            [WebGet]
            int Mul(int a, int b);
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜