Created
July 4, 2012 23:27
-
-
Save pmhsfelix/3050060 to your computer and use it in GitHub Desktop.
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
[ServiceContract] | |
internal class TheService | |
{ | |
[WebInvoke(UriTemplate = "*", Method = "*")] | |
public void TheOperation(Stream s) | |
{ | |
byte[] buf = new byte[4*1024]; | |
int blen; | |
int len = 0; | |
while ((blen = s.Read(buf, 0, buf.Length))>0) | |
{ | |
len += blen; | |
} | |
Console.WriteLine("Post with {0} bytes and '{1}' content type",len,WebOperationContext.Current.IncomingRequest.ContentType); | |
} | |
} | |
internal class RawContentTypeMapper : WebContentTypeMapper | |
{ | |
public override WebContentFormat GetMessageFormatForContentType(string contentType) | |
{ | |
return WebContentFormat.Raw; | |
} | |
} | |
class Program | |
{ | |
private static Binding GetLocalBinding() | |
{ | |
var b = new WebHttpBinding(); | |
var elems = b.CreateBindingElements(); | |
var ee = elems.Find<WebMessageEncodingBindingElement>(); | |
ee.ContentTypeMapper = new RawContentTypeMapper(); | |
return new CustomBinding(elems); | |
} | |
private static Binding GetRelayBinding() | |
{ | |
var b = new WebHttpRelayBinding(EndToEndWebHttpSecurityMode.Transport, RelayClientAuthenticationType.None); | |
var elems = b.CreateBindingElements(); | |
var ee = elems.Find<WebMessageEncodingBindingElement>(); | |
ee.ContentTypeMapper = new RawContentTypeMapper(); | |
return new CustomBinding(elems); | |
} | |
static void Main(string[] args) | |
{ | |
var host = new WebServiceHost(typeof (TheService)); | |
var localEndpoint = host.AddServiceEndpoint(typeof (TheService), GetLocalBinding(), | |
"http://locahost:8080"); | |
var relayEndpoint = host.AddServiceEndpoint(typeof(TheService), GetRelayBinding(), | |
"https://{removed}.servicebus.windows.net/webapi/"); | |
relayEndpoint.Behaviors.Add(new TransportClientEndpointBehavior | |
{ | |
TokenProvider = | |
TokenProvider.CreateSharedSecretTokenProvider("owner", "...") | |
}); | |
host.Open(); | |
Console.WriteLine("host is opened"); | |
var client = new HttpClient(); | |
var resp = client.PostAsync("http://localhost:8080", new StringContent("Hello")).Result; | |
Console.WriteLine(resp.StatusCode); | |
resp = client.PostAsync("https://{removed}.servicebus.windows.net/webapi/", new StringContent("Hello")).Result; | |
Console.WriteLine(resp.StatusCode); | |
resp = client.PostAsJsonAsync("http://localhost:8080", "hello").Result; | |
Console.WriteLine(resp.StatusCode); | |
resp = client.PostAsJsonAsync("https://{removed}.servicebus.windows.net/webapi/", "hello").Result; | |
Console.WriteLine(resp.StatusCode); | |
resp = client.PostAsXmlAsync("http://localhost:8080", "hello").Result; | |
Console.WriteLine(resp.StatusCode); | |
resp = client.PostAsXmlAsync("https://{removed}.servicebus.windows.net/webapi/", "hello").Result; | |
Console.WriteLine(resp.StatusCode); | |
Console.WriteLine("Press key"); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment