Advertisement
Guest User

wcf custom router function test

a guest
Mar 24th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.ServiceModel.Channels;
  4. using System.ServiceModel.Web;
  5. using System.Net;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Activation;
  8. using System.ServiceModel.Description;
  9.  
  10. namespace WcfCustomRouterFunctionTest
  11. {
  12.  
  13.     internal class Program
  14.     {
  15.         private static void Main(string[] args)
  16.         {
  17.             var baseUrl = "http://localhost:9999/";
  18.             var serviceName = "Test";
  19.             var f = new MyFactory(baseUrl, serviceName);
  20.             var url = baseUrl + serviceName + TestService.ROUTER_URL;
  21.             using (f.SetupService())
  22.             {
  23.                 var webClient = new WebClient();
  24.                 using (var upstream = webClient.OpenWrite(url, "POST"))
  25.                 {
  26.                     using (var writer = new StreamWriter(upstream))
  27.                     {
  28.                         writer.Write("{ \"test\" : \"json\" }");
  29.                     }
  30.                 }
  31.             }
  32.  
  33.             Console.WriteLine("Done!");
  34.             Console.ReadKey();
  35.         }
  36.        
  37.         public class MyFactory : ServiceHostFactory
  38.         {
  39.             private readonly string baseUrl;
  40.  
  41.             private readonly string serviceName;
  42.  
  43.             public MyFactory(string baseUrl, string serviceName)
  44.             {
  45.                 this.baseUrl = baseUrl;
  46.                 this.serviceName = serviceName;
  47.             }
  48.  
  49.             public IDisposable SetupService()
  50.             {
  51.                 var serviceAddress = new Uri(baseUrl + serviceName);
  52.                 ServiceHost host = this.CreateServiceHost(typeof (TestService), new[] {serviceAddress});
  53.                 var basicHttpBinding = new WebHttpBinding();
  54.                 ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof (ITestService), basicHttpBinding, string.Empty);
  55.                 endpoint.Behaviors.Add(new WebHttpBehavior());
  56.                 host.Open();
  57.                 return host;
  58.             }
  59.         }
  60.  
  61.         [ServiceContract]
  62.         public interface ITestService
  63.         {
  64.             [OperationContract]
  65.             [WebInvoke(Method = "POST",
  66.                 UriTemplate = TestService.ROUTER_URL,
  67.                 BodyStyle = WebMessageBodyStyle.Wrapped)]
  68.             Message Router(Message message);
  69.         }
  70.  
  71.         public class TestClass
  72.         {
  73.             public string test { get; set; }
  74.         }
  75.  
  76.         public class TestService : ITestService
  77.         {
  78.             public const string ROUTER_URL = "/Router/*";
  79.  
  80.             public Message Router(Message message)
  81.             {
  82.                
  83.                 TestClass testClassInstance;
  84.                 using (var reader = new StreamReader(message.GetBody<Stream>()))
  85.                 {
  86.                     testClassInstance = Newtonsoft.Json.JsonConvert.DeserializeObject<TestClass>(reader.ReadToEnd());
  87.                 }
  88.                
  89.                 return WebOperationContext.Current.CreateJsonResponse(new { Whatever = "yes"});
  90.             }
  91.         }
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement