- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题
我有一个新的 WCF
服务。其中有一个 Interface
和只有两个方法的相关类,如以下代码所示。我在其中一种方法中使用自定义类 CompanyDetail
但出现问题,我在我的客户端项目中引用此服务但没有任何效果。服务已连接但无法在代码中使用。
我尝试过的
namespace IntelliWcfService
{
[ServiceContract]
public interface IIntelliService
{
[OperationContract]
CompanyDetail GetCompanyDetails();
[OperationContract]
string Get(int value);
}
}
接口(interface)实现
namespace IntelliWcfService
{
public class IntelliService : IIntelliService
{
public CompanyDetail GetCompanyDetails()
{
try
{
var company = new CompanyDetail
{
CompanyName = "Visual Labs Pakistan",
City = "Multan",
Contact1 = "0306-8513103",
Contact2 = "",
Country = "Pakistan",
CountryCode = "+92",
Email = "VisualLabs@gmail.com",
NTN = "0007923-7",
PostalCode = "60000",
Street = "Street 2 Near Bypass Multan",
Type = "Software Technology"
};
return company;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public string Get(int value)
{
return "Connected";
}
}
}
CompanyDetail 类
using System.Runtime.Serialization;
namespace IntelliWcfService.Models
{
[DataContract]
public class CompanyDetail
{
private string _companyName;
private string _ntn;
private string _type;
private string _country;
private string _countryCode;
private string _city;
private string _street;
private string _postalCode;
private string _contact1;
private string _contact2;
private string _email;
[DataMember]
public string CompanyName
{
get => _companyName;
set => _companyName = value;
}
[DataMember]
public string NTN
{
get => _ntn;
set => _ntn = value;
}
[DataMember]
public string Type
{
get => _type;
set => _type = value;
}
[DataMember]
public string Country
{
get => _country;
set => _country = value;
}
[DataMember]
public string CountryCode
{
get => _countryCode;
set => _countryCode = value;
}
[DataMember]
public string City
{
get => _city;
set => _city = value;
}
[DataMember]
public string Street
{
get => _street;
set => _street = value;
}
[DataMember]
public string PostalCode
{
get => _postalCode;
set => _postalCode = value;
}
[DataMember]
public string Contact1
{
get => _contact1;
set => _contact1 = value;
}
[DataMember]
public string Contact2
{
get => _contact2;
set => _contact2 = value;
}
[DataMember]
public string Email
{
get => _email;
set => _email = value;
}
}
}
我已经尝试使用 [DataMember]
装饰器自动属性并且喜欢这个完整的 Prop 。但仍然没有任何效果。并在我的客户端中获取它。
结果
我已经在这里寻求帮助,但似乎我没有做错任何事。
Adding classes to WCF service library
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
观察
当我删除 GetCompnyDetails
的这种方法时,一切正常,我可以在我的客户端中使用其他方法。同样在过去,我曾经在服务中使用 EntityFramework
自动生成的模型,并且过去工作正常。
预期行为
显然我应该能够在我的客户端项目中使用所有方法。而这个自定义类应该会导致问题。
添加服务引用对话框
最佳答案
我用你的代码创建了一个 WCF 服务,它运行成功。
namespace ConsoleApp37
{
[ServiceContract]
public interface IIntelliService
{
[OperationContract]
CompanyDetail GetCompanyDetails();
[OperationContract]
string Get(int value);
}
[DataContract]
public class CompanyDetail
{
private string _companyName;
private string _ntn;
private string _type;
private string _country;
private string _countryCode;
private string _city;
private string _street;
private string _postalCode;
private string _contact1;
private string _contact2;
private string _email;
[DataMember]
public string CompanyName
{
get => _companyName;
set => _companyName = value;
}
[DataMember]
public string NTN
{
get => _ntn;
set => _ntn = value;
}
[DataMember]
public string Type
{
get => _type;
set => _type = value;
}
[DataMember]
public string Country
{
get => _country;
set => _country = value;
}
[DataMember]
public string CountryCode
{
get => _countryCode;
set => _countryCode = value;
}
[DataMember]
public string City
{
get => _city;
set => _city = value;
}
[DataMember]
public string Street
{
get => _street;
set => _street = value;
}
[DataMember]
public string PostalCode
{
get => _postalCode;
set => _postalCode = value;
}
[DataMember]
public string Contact1
{
get => _contact1;
set => _contact1 = value;
}
[DataMember]
public string Contact2
{
get => _contact2;
set => _contact2 = value;
}
[DataMember]
public string Email
{
get => _email;
set => _email = value;
}
}
public class IntelliService : IIntelliService
{
public CompanyDetail GetCompanyDetails()
{
try
{
var company = new CompanyDetail
{
CompanyName = "Visual Labs Pakistan",
City = "Multan",
Contact1 = "0306-8513103",
Contact2 = "",
Country = "Pakistan",
CountryCode = "+92",
Email = "VisualLabs@gmail.com",
NTN = "0007923-7",
PostalCode = "60000",
Street = "Street 2 Near Bypass Multan",
Type = "Software Technology"
};
return company;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public string Get(int value)
{
return "Connected";
}
}
class Program
{
static void Main(string[] args)
{
// Step 1: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");
// Step 2: Create a ServiceHost instance.
ServiceHost selfHost = new ServiceHost(typeof(IntelliService), baseAddress);
try
{
// Step 3: Add a service endpoint.
selfHost.AddServiceEndpoint(typeof(IIntelliService), new WSHttpBinding(), "CalculatorService");
// Step 4: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Step 5: Start the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
// Close the ServiceHost to stop the service.
Console.WriteLine("Press <Enter> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
这是服务器端代码。
客户端根据服务器的端点生成代理类来调用服务。
我成功调用了 GetCompanyDetails 方法并打印了 City 属性的值。
更新
根据您的描述,我创建了一个 UWP 程序来调用 WCF,如下所示:
这是 ButtonBase_OnClick:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService");
ServiceReference1.IntelliServiceClient intelliServiceClient = new ServiceReference1.IntelliServiceClient(binding,endpointAddress);
ServiceReference1.CompanyDetail companyDetail= await intelliServiceClient.GetCompanyDetailsAsync();
MessageDialog message = new MessageDialog(companyDetail.City);
await message.ShowAsync();
}
关于C# 自定义类在 WCF 服务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738394/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!