Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Web;
- using System.Net;
- using System.ServiceModel;
- using System.ServiceModel.Activation;
- using System.ServiceModel.Description;
- namespace WcfCustomRouterFunctionTest
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- var baseUrl = "http://localhost:9999/";
- var serviceName = "Test";
- var f = new MyFactory(baseUrl, serviceName);
- var url = baseUrl + serviceName + TestService.ROUTER_URL;
- using (f.SetupService())
- {
- var webClient = new WebClient();
- using (var upstream = webClient.OpenWrite(url, "POST"))
- {
- using (var writer = new StreamWriter(upstream))
- {
- writer.Write("{ \"test\" : \"json\" }");
- }
- }
- }
- Console.WriteLine("Done!");
- Console.ReadKey();
- }
- public class MyFactory : ServiceHostFactory
- {
- private readonly string baseUrl;
- private readonly string serviceName;
- public MyFactory(string baseUrl, string serviceName)
- {
- this.baseUrl = baseUrl;
- this.serviceName = serviceName;
- }
- public IDisposable SetupService()
- {
- var serviceAddress = new Uri(baseUrl + serviceName);
- ServiceHost host = this.CreateServiceHost(typeof (TestService), new[] {serviceAddress});
- var basicHttpBinding = new WebHttpBinding();
- ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof (ITestService), basicHttpBinding, string.Empty);
- endpoint.Behaviors.Add(new WebHttpBehavior());
- host.Open();
- return host;
- }
- }
- [ServiceContract]
- public interface ITestService
- {
- [OperationContract]
- [WebInvoke(Method = "POST",
- UriTemplate = TestService.ROUTER_URL,
- BodyStyle = WebMessageBodyStyle.Wrapped)]
- Message Router(Message message);
- }
- public class TestClass
- {
- public string test { get; set; }
- }
- public class TestService : ITestService
- {
- public const string ROUTER_URL = "/Router/*";
- public Message Router(Message message)
- {
- TestClass testClassInstance;
- using (var reader = new StreamReader(message.GetBody<Stream>()))
- {
- testClassInstance = Newtonsoft.Json.JsonConvert.DeserializeObject<TestClass>(reader.ReadToEnd());
- }
- return WebOperationContext.Current.CreateJsonResponse(new { Whatever = "yes"});
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement