您的位置:首页 > 移动开发

First WCF App (Host in Console Application)

2011-11-02 23:59 232 查看
File Structure

Program.cs
Service.cs
IService.cs
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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: