Last active
August 29, 2015 14:07
-
-
Save aalinat/43585b2f9c5b2049ca3c to your computer and use it in GitHub Desktop.
wcf self hosted soap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.ServiceModel; | |
using System.ServiceModel.Description; | |
using System.ServiceModel.Web; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace WCFServer | |
{ | |
[ServiceContract] | |
public interface IHelloWorldService | |
{ | |
[OperationContract] | |
string SayHello(string name); | |
} | |
public class HelloWorldService : IHelloWorldService | |
{ | |
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] | |
public string SayHello(string name) | |
{ | |
return string.Format("Hello, {0}", name); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Uri baseAddress = new Uri("http://localhost:8080/hello"); | |
// Create the ServiceHost. | |
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) | |
{ | |
ServiceEndpoint ep2 = host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), ""); | |
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>(); | |
stp.HttpHelpPageEnabled = true; | |
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); | |
// If not, add one | |
if (smb == null) | |
smb = new ServiceMetadataBehavior(); | |
smb.HttpGetEnabled = true; | |
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; | |
host.Description.Behaviors.Add(smb); | |
host.Open(); | |
Console.WriteLine("The service is ready at {0}", baseAddress); | |
Console.WriteLine("Press <Enter> to stop the service."); | |
Console.ReadLine(); | |
// Close the ServiceHost. | |
host.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment