First WCF App (Host in Console Application)

File Structure

  1. Program.cs
  2. Service.cs
  3. IService.cs
  4. App.config
1. App.config

Here defines two approach to access WCF service :
1> (Able to access from browser and WcfTestClient.exe).   http://127.0.0.1:9090/myservice/ 
    -httpGetEnabled="true"
2> (Able to access via WcfTestClient.exe) .    http://127.0.0.1:9090/myservice/mex
  -httpGetEnabled="true" + mex end point

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      
      <service behaviorConfiguration="metadataBehavior" 
               name="ConsoleApplication2.Service">
        <host>
          <baseAddresses>
            <add baseAddress="http://127.0.0.1:9090/myservice/"/>
          </baseAddresses>
        </host>
        <endpoint address="" 
                  binding="wsHttpBinding"
                  contract="ConsoleApplication2.IService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata
            httpGetEnabled="true"
            />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

2.IService.cs and Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ConsoleApplication2
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface IService
    {
           [OperationContract]
        string GetData(string input);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ConsoleApplication2
{
    
    public class Service:IService
    {
     
        public string GetData(string input)
        {
            return "Your input is :" + input;
        }
    }
}

3. Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(ConsoleApplication2.Service)))
            {
                host.Open();
                Console.Write("Service is up and running");
                Console.ReadKey();
                host.Close();
            }

        }
    }
}


Note: Instead of using App.config, we can also config our WCF in code behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            start();
        }

        static void start()
        {
            //this does the hosting
            using (ServiceHost host = new ServiceHost(typeof(ConsoleApplication2.Service),
                new Uri("http://127.0.0.1:9090/myservice/")))
            {
                //this lets you browse to the WSDL:
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                //smb.HttpGetUrl = new Uri("http://127.0.0.1:9090/myservice/Meta");
                host.Description.Behaviors.Add(smb);

                //This is the bit that handles HTTP
                WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
                Binding mex = MetadataExchangeBindings.CreateMexHttpBinding();

                //These are the endpoints on that HTTP server
                host.AddServiceEndpoint("ConsoleApplication2.IService", ws, ""); //one to actually do things
                host.AddServiceEndpoint(typeof(IMetadataExchange), mex, "mex"); //and one to provide metadata

                // start listening
                host.Open();
                Console.Write("Service is up and running");
                Console.ReadKey();
                host.Close();
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值