Reputation: 7532
I have a simple client app that talks to a self hosted web api:
class Program
{
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
client.BaseAddress = new Uri("http://localhost:8080");
LoadForeman();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
static void LoadForeman()
{
HttpResponseMessage resp = client.GetAsync("api/foreman").Result;
resp.EnsureSuccessStatusCode();
var foreman = resp.Content.ReadAsAsync<IEnumerable<SelfHost.Foreman>>().Result;
foreach (var f in foreman)
{
Console.WriteLine("{0} {1}", f.ForeBadge, f.ForeName);
}
}
}
How do I pass parameters (strings) from the client to the service?
EDIT: WEB API SERVICE
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
//describes how to access API via HTTP
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
Upvotes: 0
Views: 1213
Reputation: 11
I realize that this is an old post, but you can pass information to the controller with a custom DelegatingHandler.
i.e:
_config = New HttpSelfHostConfiguration(myUrl)
_config.MessageHandlers.add(New MyHandler(cs))
Public Class WebApiHandler
Inherits System.Net.Http.DelegatingHandler
Private ReadOnly _cs As String
Public Sub New(ByVal cs as String)
_cs = cs
End Sub
Protected Overrides Function SendAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
request.Properties("ConnectionString") = _cs
Return MyBase.SendAsync(request, cancellationToken)
End Function
End Class
Upvotes: 1