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
2.IService.cs and Service.cs
3. Program.cs
Note: Instead of using App.config, we can also config our WCF in code behind.
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(); } } } }
相关文章推荐
- The virtual machine is in use by an application on your host computer
- [转载]Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application
- Effective testing in console application
- C# Clipboard in Console Application
- Host Python In your Application with IronPython2.0.1 (一): 创建交互环境
- How to debug c++ console application with argument in visual studio 2012
- RCP开发运行时Plug-in TD was unable to load class td.app.Application
- C# Clipboard in Console Application
- Host a WCF Service in a Managed Application
- This application has requested the Runtime to terminate it in an unusual way. Please contact the app
- Show Console Window in Windows Application
- 虚拟机提示the virtual machine is in use by an application on your host computer的解决方案
- the virtual machine is in use by an application on your host computer
- In-App Purchase--Adding a Store to Your Application[5]
- the virtual machine is in use by an application on your host computer
- how to use ocx control in console app.
- 警告:Initalizing 'AppDelegate * __strong' with an expression of incompatible type ''id<UIApplication..
- Host Python in your application with IronPython 2.0.1(二): 互相访问
- Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application
- IPHONE - GSRegisterPurpleNamedPort SIGABRT in UIApplicationMain Before App Delegate Gets to Run Any Code