【.net core】.net core 6.0添加WCF服务引用

chatgpt误我!
在 .NET Core 6.0 (.NET 6) 中,调用 WCF 服务 是完全支持的
只要服务使用的是 basicHttpBinding 或类似 HTTP 协议的绑定(如 wsHttpBinding,但不推荐)
.NET Core不支持 net.tcp,只能用http形式。
上述说法完全错误,.net 支持两种形式,在调用的时候传入即可。

var client = new ServiceDelegateClient(
    //ServiceDelegateClient.EndpointConfiguration.BasicHttpBinding_Imyservice,
    ServiceDelegateClient.EndpointConfiguration.NetTcpBinding_Imyservice
    new EndpointAddress("net.tcp://127.0.0.1:8001/myservice")

.net core调用WCF服务有两种方式
一种是添加服务引用到项目中,这样可以直接调用WCF的方法,假如我们是调用第三方的wcf,可能就不知道第三方有哪些方法,所以添加服务引用到项目中,就可以直接调用
另一种是不添加服务引用到项目中,这就需要在项目中定义指定的接口,然后通过ChannelFactory 直接调用,注意:因为此时我们项目中没有wcf的方法(比喻可能不准确,大概意思就是我们项目中没有这个方法,添加服务引用就是有了这个方法),所以一定要定义接口。
此文档只讲解添加服务引用到项目中形式,也就是上面说的第一种。

  1. 确认已经有了WCF项目,并且能够引用
  2. 在需要引用的项目中,添加服务引用,以vs2022为例:右键项目,选择添加–服务引用
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在上图输入uri链接,这个链接可以在WCF项目的配置文件中找到
    在这里插入图片描述

本地调试中我用的是这个,线上还有待试验,另外是否需要添加http形式也有待试验
然后一直下一步直到成功
在这里插入图片描述
之后可以在这里看到已经配置成功
4. 调用WCF的方法

var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://127.0.0.1:8001/myservice");
CustomerServiceDelegateClient client = new CustomerServiceDelegateClient(binding,endpoint);
try
{
    var res= await client.AddTrackDetailsAsync(item.TbsId, item.TrackDetails.ToArray(), null);
    return res.AddTrackDetailsResult;
}
finally
{
    client.Close();
}
  1. 调试:启动本地的wcf项目,启动项目,正常情况下可以跑到wcf项目
  2. 问题: 可能调不到wcf项目,要看配置和调用是否一致,比如wcf中配置用tcp,调用也是用tcp
 <service name="myservice">
  <endpoint address="" binding="basicHttpBinding" contract="myservice" />
    <host>
          <baseAddresses>
            <add baseAddress="http://127.0.0.1:8001/myservice" />
          </baseAddresses>
        </host>
      </service>

已有的不需要删除,只需要添加http的就可以

### 如何在 WPF (.NET 6.0) 中集成和使用 WCF 服务 #### 创建并配置 WCF 客户端代理 为了使 WPF 应用程序能够调用 WCF 服务,通常需要创建一个客户端代理来简化与服务之间的交互。这可以通过 `svcutil.exe` 工具自动生成,也可以通过编程方式手动定义。 对于现代项目推荐采用依赖注入的方式注册服务接口,并利用 ChannelFactory 动态建立连接: ```csharp using System; using System.ServiceModel; public class MyServiceClient : IDisposable { private readonly IMyService _channel; public MyServiceClient() { var binding = new BasicHttpBinding(); var endpointAddress = new EndpointAddress("http://example.com/MyService.svc"); var factory = new ChannelFactory<IMyService>(binding, endpointAddress); _channel = factory.CreateChannel(); } public void Dispose(){ ((IClientChannel)_channel).Close(); } } ``` 此方法允许更灵活地管理生命周期以及处理异常情况[^1]。 #### 添加对 Windows Compatibility 的支持 由于 .NET Core 和后续版本默认不包含完整的 WCF 实现,因此当目标框架设置为 .NET 6 或更高时,需显式安装 NuGet 包 `System.ServiceModel.Wcf` 及其相关依赖项以启用必要的功能集。此外还可以考虑加入 `Microsoft.Windows.Compatibility` 来获取更多传统 API 支持[^2]。 #### 设计 UI 并绑定命令逻辑 假设有一个简单的按钮点击事件触发远程操作,则可以在 XAML 文件里声明如下结构: ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Grid> <!-- Button to invoke service method --> <Button Content="Call Service" Command="{Binding CallServiceCommand}" /> </Grid> </Window> ``` 对应的 ViewModel 类实现 INotifyPropertyChanged 接口并与视图关联起来: ```csharp using CommunityToolkit.Mvvm.ComponentModel; // For ObservableObject base class using CommunityToolkit.Mvvm.Input; // For RelayCommand definition public partial class MainViewModel : ObservableObject { [ICommand] async Task CallServiceAsync() { using(var client = new MyServiceClient()){ try{ await client.CallRemoteMethodAsync(); // Replace with actual call Console.WriteLine("Service called successfully."); }catch(Exception ex){ MessageBox.Show($"Error occurred while calling service:\n{ex.Message}"); } } } } ``` 这里采用了 MVVM 模式分离关注点,使得界面更加易于维护和发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值