Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually the easiest way is to use the built-in WCF stuff (.Net 3.5)... To do this you create a interface for your 'WCF' service that contains one or more methods that return Stream:</p> <pre><code>[ServiceContract] public interface IService { [OperationContract] [WebInvoke(UriTemplate = "/{*arguments}", Method="GET", BodyStyle=WebMessageBodyStyle.Bare)] Stream Get(string arguments); } </code></pre> <p>You can define several methods and arguments and let WFC do the work, or as the example above, push everything into a single method. The resulting implementation can access the full Uri and query parameters as follows:</p> <pre><code>public class ServiceType : IService { public Stream Get(string arguments) { UriTemplateMatch uriInfo = WebOperationContext.Current.IncomingRequest.UriTemplateMatch; WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; MemoryStream rawResponse = new MemoryStream(); TextWriter response = new StreamWriter(rawResponse, Encoding.UTF8); response.Write("&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt;&lt;body&gt;"); response.Write("&lt;b&gt;Path&lt;/b&gt;: {0}&lt;br/&gt;", arguments); response.Write("&lt;b&gt;RequestUri&lt;/b&gt;: {0}&lt;br/&gt;", uriInfo.RequestUri); response.Write("&lt;b&gt;QueryParameters&lt;/b&gt;: {0}&lt;br/&gt;", uriInfo.QueryParameters.ToString()); response.Write("&lt;/body&gt;&lt;/html&gt;"); response.Flush(); rawResponse.Position = 0; return rawResponse; } } </code></pre> <p>Now all you have to do is start up the WCF web/http self-host ...</p> <pre><code>static void Main() { Uri baseAddress = new Uri("http://localhost:8000/"); WebServiceHost svcHost = new WebServiceHost(typeof(ServiceType)); ServiceEndpoint svcEndpoint = svcHost.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), baseAddress); svcEndpoint.Behaviors.Add(new WebHttpBehavior()); svcHost.Open(); Console.WriteLine("Press enter to quit..."); Console.ReadLine(); svcHost.Close(); } </code></pre> <p>NOTE: for the above example to work on Vista/Win7 you need to grant permissions with the following command-line:</p> <pre><code>netsh http add urlacl url=http://+:8000/ user=DOMAIN\USER </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload