在 net core 2.0 中使用外部 WCF
consume external WCF in netcore 2.0
我已经阅读了一些相关内容,但我有点困惑,所以我想知道我所做的是否正确。
我在 netcore 2.0 中创建了一个 WEBAPI,我必须使用外部 WCF(框架 4.5),我做了一个非常简单的测试,为此我创建了一个 WCF,我可以在 WEBAPI 中添加服务引用,然后我消耗了它。
所以这个例子工作正常
public async Task<WCFMySevrice.CompositeType> IndexTest2(WCFMySevrice.CompositeType value)
{
//Creating Client object of Service Reference
WCFMySevrice.MyServiceClient client = new WCFMySevrice.MyServiceClient();
//Call the method from WCF Service
WCFMySevrice.CompositeType result = await client.GetDataUsingDataContractAsync(value);
return result;
}
我的问题是,目前我的 WCF 的 url 是“http://localhost:55203/MyService.svc”,但是如果我想在生产环境中部署我的应用程序,URL 将会改变,是有配置端点的方法吗?我读过它,但我知道这是不可能的,我需要用 "WCF svcutil tool overview" 创建一个代理 class,但我不清楚。如果我使用它,我是否不再需要服务参考?
任何建议都会有所帮助。
当您在visual studio发布wcf服务时,服务元数据地址由两部分组成。
IIS 将提供以下表格。
地址的其余部分由配置组成。 “/数据”
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="mybehavior">
<endpoint address="Data" binding="wsHttpBinding" contract="WcfService1.IService1" bindingConfiguration="http"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
以上服务地址为
然后我们通过 Microsoft WCF Web 服务引用提供程序生成客户端代理 class。
即我们可以通过配置文件指定服务端点地址。
如果有什么我可以帮忙的,请随时告诉我。
Abraham Qian 感谢您的帮助。
由于 net core 对我来说是新的,我很困惑,因为在我只更改 web.config 中的端点之前,我认为我没有完全清楚这个概念,但我所做的工作
看我这样做了:
- 在appsettings.json
{
"WCFMyService": {
"EndpointUrl": "http://localhost:55203/MyService.svc"
}
}
- 启动中class
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MyServiceOptions>(Configuration.GetSection("WCFMyService"));
services.AddMvc()
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
}
- 我的控制器
namespace APITest_CallWCF.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading.Tasks;
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IOptions<MyServiceOptions> myOptions;
public ValuesController(IOptions<MyServiceOptions> options)
{
this.myOptions = options;
}
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// POST api/values
[HttpPost]
public void Post([FromBody]WCFMySevrice.CompositeType value)
{
if (ModelState.IsValid)
{
IndexText3(value);
}
}
public async Task<WCFMySevrice.CompositeType> IndexText3(WCFMySevrice.CompositeType value)
{
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress(myOptions.Value.EndpointUrl);
WCFMySevrice.MyServiceClient wcfClient = new WCFMySevrice.MyServiceClient(basicHttpBinding, endpointAddress);
//Call the method from WCF Service
WCFMySevrice.CompositeType result = await wcfClient.GetDataUsingDataContractAsync(value);
return result;
}
}
}
我已经阅读了一些相关内容,但我有点困惑,所以我想知道我所做的是否正确。
我在 netcore 2.0 中创建了一个 WEBAPI,我必须使用外部 WCF(框架 4.5),我做了一个非常简单的测试,为此我创建了一个 WCF,我可以在 WEBAPI 中添加服务引用,然后我消耗了它。 所以这个例子工作正常
public async Task<WCFMySevrice.CompositeType> IndexTest2(WCFMySevrice.CompositeType value)
{
//Creating Client object of Service Reference
WCFMySevrice.MyServiceClient client = new WCFMySevrice.MyServiceClient();
//Call the method from WCF Service
WCFMySevrice.CompositeType result = await client.GetDataUsingDataContractAsync(value);
return result;
}
我的问题是,目前我的 WCF 的 url 是“http://localhost:55203/MyService.svc”,但是如果我想在生产环境中部署我的应用程序,URL 将会改变,是有配置端点的方法吗?我读过它,但我知道这是不可能的,我需要用 "WCF svcutil tool overview" 创建一个代理 class,但我不清楚。如果我使用它,我是否不再需要服务参考?
任何建议都会有所帮助。
当您在visual studio发布wcf服务时,服务元数据地址由两部分组成。 IIS 将提供以下表格。
地址的其余部分由配置组成。 “/数据”
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="mybehavior">
<endpoint address="Data" binding="wsHttpBinding" contract="WcfService1.IService1" bindingConfiguration="http"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
以上服务地址为
然后我们通过 Microsoft WCF Web 服务引用提供程序生成客户端代理 class。
即我们可以通过配置文件指定服务端点地址。
如果有什么我可以帮忙的,请随时告诉我。
Abraham Qian 感谢您的帮助。 由于 net core 对我来说是新的,我很困惑,因为在我只更改 web.config 中的端点之前,我认为我没有完全清楚这个概念,但我所做的工作
看我这样做了:
- 在appsettings.json
{ "WCFMyService": { "EndpointUrl": "http://localhost:55203/MyService.svc" } }
- 启动中class
public void ConfigureServices(IServiceCollection services) { services.Configure<MyServiceOptions>(Configuration.GetSection("WCFMyService")); services.AddMvc() .AddXmlSerializerFormatters() .AddXmlDataContractSerializerFormatters(); }
- 我的控制器
namespace APITest_CallWCF.Controllers { using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using System.Collections.Generic; using System.ServiceModel; using System.Threading.Tasks; [Route("api/[controller]")] public class ValuesController : Controller { private readonly IOptions<MyServiceOptions> myOptions; public ValuesController(IOptions<MyServiceOptions> options) { this.myOptions = options; } // GET api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // POST api/values [HttpPost] public void Post([FromBody]WCFMySevrice.CompositeType value) { if (ModelState.IsValid) { IndexText3(value); } } public async Task<WCFMySevrice.CompositeType> IndexText3(WCFMySevrice.CompositeType value) { BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); EndpointAddress endpointAddress = new EndpointAddress(myOptions.Value.EndpointUrl); WCFMySevrice.MyServiceClient wcfClient = new WCFMySevrice.MyServiceClient(basicHttpBinding, endpointAddress); //Call the method from WCF Service WCFMySevrice.CompositeType result = await wcfClient.GetDataUsingDataContractAsync(value); return result; } } }