あなたがサービスを利用するためにWCFを使用することができます。
[ServiceContract]
public interface IDogService
{
[OperationContract]
[WebGet(UriTemplate = "/api/dog")]
IEnumerable<Dog> List();
}
public class DogServiceClient : ClientBase<IDogService>, IDogService
{
public DogServiceClient(string endpointConfigurationName) : base(endpointConfigurationName)
{
}
public IEnumerable<Dog> List()
{
return Channel.List();
}
}
そして、あなたのコントローラでそれを消費することができます:
public class HomeController : Controller
{
public HomeController()
{
}
public ActionResult List()
{
var service = new DogServiceClient("YourEndpoint");
var dogs = service.List();
return View(dogs);
}
}
そして、あなたのweb.configファイルで、あなたのエンドポイントの設定を置く:
<system.serviceModel>
<client>
<endpoint address="http://localhost/DogService" binding="webHttpBinding"
bindingConfiguration="" behaviorConfiguration="DogServiceConfig"
contract="IDogService" name="YourEndpoint" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="DogServiceConfig">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
+1私はそれを私のウェブアプリケーションのそれとまったく同じです –
これはすばらしく見えますが、私は非同期/待っているものを全く動かすことができません。 IDEはこれらのキーワードが気に入らない。 .NET 4.0を使用しており、NuGetを使用してWeb APIクライアントライブラリをインストールしました。 –
@ GlennArndtあなたがいくつかのアクションを取らない限り、.NET 4.0ではasync/awaitを使用できません。このブログの記事をご覧ください:http://blogs.msdn.com/b/bclteam/archive/2012/10/22/using-async-await-without-net-framework-4-5.aspx非同期コントローラのアクションを活用できます非同期ではなく、キーワードを待っていますが、それは面倒です。 – tugberk