Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames committed Jan 22, 2018
1 parent c38d50a commit 6443615
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions docs/zh-cn/service-simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ public interface ISimpleInterface

## 服务设置

需要安装`Shriek.ServiceProxy.Http.Server`类库
首先,实现这个接口定义一个类,这个类只是简单的类:

```csharp

public class SimpleInterface : ISimpleInterface
{
public async Task<string> Test(string sth)
{
return await Task.FromResult("server: " + sth);
}
}

```

只需要简单地在`Startup.cs`文件中的`ConfigureServices`方法中的AddMVC()后面加入以下一行:
接下来,我们要把这个实现类设置成一个网络服务,只需要简单地在`Startup.cs`文件中的`ConfigureServices`方法中的AddMVC()后面加入以下一行:

```csharp
//Startup.cs
Expand All @@ -32,25 +44,13 @@ public void ConfigureServices(IServiceCollection services)
}
```

## 服务调用

需要安装`Shriek.ServiceProxy.Http`类库
此时,已经自动配置了一个可以用POST方式调用的WebApi,接受一个字符串参数,路由是用接口名+方法名+参数名生成的。

首先,实现这个接口定义一个类,这个类只是简单的类:
> 服务设置需要安装`Shriek.ServiceProxy.Http.Server`类库
```csharp

public class SimpleInterface : ISimpleInterface
{
public async Task<string> Test(string sth)
{
return await Task.FromResult("server: " + sth);
}
}

```
## 服务调用

然后需要在`Startup.cs`类中简单地在`IServiceCollection`中注册我们的客户端组件:
只需要需要在`Startup.cs`类中简单地在`IServiceCollection`中注册我们的客户端组件:

```csharp

Expand All @@ -60,9 +60,10 @@ services.AddWebApiProxy(opt =>
{
opt.AddService<ISimpleInterface>("http://localhost:5000");
});

```

> 服务调用需要安装`Shriek.ServiceProxy.Http`类库
调用时可以直接从`IServiceProvider`获取实例,并且直接调用。

```csharp
Expand All @@ -71,6 +72,7 @@ var provider = services.BuildServiceProvider();
var simpleService = provider.GetService<ISimpleInterface>();
var result = await simpleService.Test("hello word!"); // -->result = "server: hello word!"
```

---

当在ASP.NET Core中使用时,只需在`Startup.cs`类中的`ConfigureServices`方法中给`services`变量通过以上方法注册,即可在控制器注入客户端了。
Expand Down

0 comments on commit 6443615

Please sign in to comment.