Api 实现一个代理来调用Rest服务
我创建了一个RESTWebAPI,现在我想从代理调用API公开的操作。我的环境是Visual Studio,C# 由于我在API中公开的操作共享URL/端点(“API/NameOfController”)的一部分,我希望通过在代理项目的app.config文件中创建一个自定义标记来引用这段URL;我将通过使用ConfigurationManager初始化代理的属性来调用它: 为此,在代理的app.config中,我在标记内编写了以下标记:Api 实现一个代理来调用Rest服务,api,rest,proxy,app-config,configurationmanager,Api,Rest,Proxy,App Config,Configurationmanager,我创建了一个RESTWebAPI,现在我想从代理调用API公开的操作。我的环境是Visual Studio,C# 由于我在API中公开的操作共享URL/端点(“API/NameOfController”)的一部分,我希望通过在代理项目的app.config文件中创建一个自定义标记来引用这段URL;我将通过使用ConfigurationManager初始化代理的属性来调用它: 为此,在代理的app.config中,我在标记内编写了以下标记: <customEndpointSegment na
<customEndpointSegment name = "/api/NameOfTheController" />
<endpoint address="http://localhost/RESTService/MovieController.cs" binding="basicHttpBinding" />
然后我定义了一个方法,该方法接受HttpResponse并返回结果,转换为Json:
T GetResult<T>(HttpResponseMessage response)
{
var content = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(content);
}
T获取结果(HttpResponseMessage响应)
{
var content=response.content.ReadAsStringAsync().Result;
返回JsonConvert.DeserializeObject(内容);
}
我从调用rest web API公开的操作的所有方法中调用前面的方法,例如:
public int InsertMovie(Movie movie)
{
HttpResponseMessage response = _client.PostAsJsonAsync<Movie>("AddMovie", movie).Result;
return GetResult<int>(response);
}
public int InsertMovie(电影)
{
HttpResponseMessage响应=_client.postsjsonAsync(“AddMovie”,movie).Result;
返回GetResult(response);
}
当该方法接受的参数不止一个时,我定义了一个类型为Movie Controller的变量来返回该对象,我在PostAsync()方法中传递这样一个变量,例如:
public List<Movie> GetMovieByProperties(string title, int year)
{
var _movieController = new MovieController(); // controller del RestService che espone le operazioni
List<Movie> returnedDvdOfMovie = _movieController .GetMovieByProperties(title, year);
HttpResponseMessage response = _client.PostAsJsonAsync<List<Movie>>("SearchMovie", returnedDvdOfMovie ).Result;
return GetResult<List<Movie>>(response);
}
公共列表GetMovieByProperties(字符串标题,整数年)
{
var _movieController=new movieController();//控制器del RestService che espone le operazioni
List returnedDvdOfMovie=\u movieController.GetMovieByProperties(标题,年份);
HttpResponseMessage response=_client.postsjsonasync(“SearchMovie”,returnedDvdOfMovie)。结果;
返回GetResult(response);
}
在代理的app.config中,我还在标记中添加了以下端点地址:
<customEndpointSegment name = "/api/NameOfTheController" />
<endpoint address="http://localhost/RESTService/MovieController.cs" binding="basicHttpBinding" />
当我测试代理的方法时,我得到以下错误:
“配置系统初始化失败”
我应该如何修复app.config以使其正常工作?
我应该用不同的HttpClient方法替换PostAsJsonAsync方法吗?我不知道如何避免为那些接受多于一个参数的方法初始化控制类的变量,我想知道我的过程是否可以接受