WCF 服务中的 C# Custom Class(server user) 在客户端不工作(但结构工作)(问题)
C# Custom Class(server user) in WCF service not working in client(but struct work)(problem)
//我的wcf服务是由控制台应用程序托管的。
console app
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
using (var host = new ServiceHost(typeof(ClassLibrary1.wcf_chating)))
{
host.Open();
Console.WriteLine("Стартуем!");
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine($"Исключение: {ex.Message}");
Console.WriteLine($"Исключение: {ex.InnerException}");
Console.WriteLine($"Исключение: {ex.TargetSite}");
Console.WriteLine($"Исключение: {ex.Source}");
Console.WriteLine($"Исключение: {ex.StackTrace}");
Console.ReadLine();
}
}
}
}
//托管在控制台中的合约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
namespace ClassLibrary1
{
[ServiceContract(CallbackContract =typeof(i_call_back))]
[ServiceKnownType(typeof(ServerUser))]
public interface Iwcf_chating
{
[OperationContract]
void connect(string name, int ID);
[OperationContract]
void disconnect(int id);
[OperationContract(IsOneWay =true)]
void senmessage(String msg,int id);
}
public interface i_call_back
{
[OperationContract(IsOneWay = true)]
void mescallback(String msg);
}
[ServiceContract(CallbackContract =typeof(i_call_back2))]
public interface Idatabase
{
[OperationContract]
void dataconnect();
[OperationContract]
DataUser Checproverka1(String text1, String text2);
[OperationContract]
DataUser Checproverka2(String text1);
[OperationContract]
void insertresults(String text1, String text2, String text3, String text4);
!!!! [OperationContract](problem metod because return custom class if i only delete this
metod i can connect with my client application )
List<ServerUser> Online_Vivod();
}
public interface i_call_back2
{
[OperationContract(IsOneWay = true)]
void mescallback2(String name);
}
}
//app.config 文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="gametime">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ClassLibrary1.wcf_chating" behaviorConfiguration="gametime">
<endpoint address="" binding="netTcpBinding" contract="ClassLibrary1.Idatabase">
</endpoint>
<endpoint address="" binding="wsDualHttpBinding" contract="ClassLibrary1.Iwcf_chating">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.65:8302" />
<add baseAddress="net.tcp://192.168.1.65:8301" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
//文件有自定义 class 和结构(客户端看到结构但看不到我的 class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace ClassLibrary1
{
[DataContract]
public class ServerUser
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public OperationContext operationContext { get; set; }
public ServerUser(OperationContext operationContext, string name, int Id)
{
this.operationContext = operationContext;
this.name = name;
this.ID = Id;
}
public ServerUser()
{
}
}
[DataContract]
public struct DataUser
{
[DataMember]
public string msg;
[DataMember]
public string name;
[DataMember]
public int Id;
public DataUser(string msg,string name,int Id)
{
this.msg = msg;
this.name = name;
this.Id = Id;
}
}
}
设置连接
connect wcf in client settings
//我猜服务接口和结构是由客户端中的服务自动生成的,但我没有在客户端自动生成文件中看到我的 class。当我 return 自定义 class 时,服务器成功编译但无法与我的客户端连接
//我也给你看部分自动文件
namespace WindowsFormsApp12.ChatService {
using System.Runtime.Serialization;
using System;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="DataUser",
Namespace="http://schemas.datacontract.org/2004/07/ClassLibrary1")]
[System.SerializableAttribute()]
public partial struct DataUser : System.Runtime.Serialization.IExtensibleDataObject,
System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private int IdField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string msgField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string nameField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public int Id {
get {
return this.IdField;
}
set {
if ((this.IdField.Equals(value) != true)) {
this.IdField = value;
this.RaisePropertyChanged("Id");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string msg {
get {
return this.msgField;
}
set {
if ((object.ReferenceEquals(this.msgField, value) != true)) {
this.msgField = value;
this.RaisePropertyChanged("msg");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string name {
get {
return this.nameField;
}
set {
if ((object.ReferenceEquals(this.nameField, value) != true)) {
this.nameField = value;
this.RaisePropertyChanged("name");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged =
this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new
System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ChatService.Idatabase",
CallbackContract=typeof(WindowsFormsApp12.ChatService.IdatabaseCallback))]
public interface Idatabase {
[System.ServiceModel.OperationContractAttribute(Action="http://temp
uri.org/Idatabase/dataconnect",
ReplyAction="http://tempuri.org/Idatabase/dataconnectResponse")]
void dataconnect();
[
.......
好的,我找到我的问题了
App.config 文件,基地址
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="ClassLibrary1.wcf_chating" behaviorConfiguration="BehConfig">
<endpoint contract="ClassLibrary1.Iwcf_chating" binding="wsDualHttpBinding"
address=""/>
<endpoint contract="ClassLibrary1.Idatabase" binding="wsDualHttpBinding"
address=""/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:4000"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="BehConfig" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
//我的wcf服务是由控制台应用程序托管的。 console app
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
using (var host = new ServiceHost(typeof(ClassLibrary1.wcf_chating)))
{
host.Open();
Console.WriteLine("Стартуем!");
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine($"Исключение: {ex.Message}");
Console.WriteLine($"Исключение: {ex.InnerException}");
Console.WriteLine($"Исключение: {ex.TargetSite}");
Console.WriteLine($"Исключение: {ex.Source}");
Console.WriteLine($"Исключение: {ex.StackTrace}");
Console.ReadLine();
}
}
}
}
//托管在控制台中的合约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
namespace ClassLibrary1
{
[ServiceContract(CallbackContract =typeof(i_call_back))]
[ServiceKnownType(typeof(ServerUser))]
public interface Iwcf_chating
{
[OperationContract]
void connect(string name, int ID);
[OperationContract]
void disconnect(int id);
[OperationContract(IsOneWay =true)]
void senmessage(String msg,int id);
}
public interface i_call_back
{
[OperationContract(IsOneWay = true)]
void mescallback(String msg);
}
[ServiceContract(CallbackContract =typeof(i_call_back2))]
public interface Idatabase
{
[OperationContract]
void dataconnect();
[OperationContract]
DataUser Checproverka1(String text1, String text2);
[OperationContract]
DataUser Checproverka2(String text1);
[OperationContract]
void insertresults(String text1, String text2, String text3, String text4);
!!!! [OperationContract](problem metod because return custom class if i only delete this
metod i can connect with my client application )
List<ServerUser> Online_Vivod();
}
public interface i_call_back2
{
[OperationContract(IsOneWay = true)]
void mescallback2(String name);
}
}
//app.config 文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="gametime">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ClassLibrary1.wcf_chating" behaviorConfiguration="gametime">
<endpoint address="" binding="netTcpBinding" contract="ClassLibrary1.Idatabase">
</endpoint>
<endpoint address="" binding="wsDualHttpBinding" contract="ClassLibrary1.Iwcf_chating">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.65:8302" />
<add baseAddress="net.tcp://192.168.1.65:8301" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
//文件有自定义 class 和结构(客户端看到结构但看不到我的 class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace ClassLibrary1
{
[DataContract]
public class ServerUser
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public OperationContext operationContext { get; set; }
public ServerUser(OperationContext operationContext, string name, int Id)
{
this.operationContext = operationContext;
this.name = name;
this.ID = Id;
}
public ServerUser()
{
}
}
[DataContract]
public struct DataUser
{
[DataMember]
public string msg;
[DataMember]
public string name;
[DataMember]
public int Id;
public DataUser(string msg,string name,int Id)
{
this.msg = msg;
this.name = name;
this.Id = Id;
}
}
}
设置连接 connect wcf in client settings
//我猜服务接口和结构是由客户端中的服务自动生成的,但我没有在客户端自动生成文件中看到我的 class。当我 return 自定义 class 时,服务器成功编译但无法与我的客户端连接 //我也给你看部分自动文件
namespace WindowsFormsApp12.ChatService {
using System.Runtime.Serialization;
using System;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="DataUser",
Namespace="http://schemas.datacontract.org/2004/07/ClassLibrary1")]
[System.SerializableAttribute()]
public partial struct DataUser : System.Runtime.Serialization.IExtensibleDataObject,
System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private int IdField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string msgField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string nameField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public int Id {
get {
return this.IdField;
}
set {
if ((this.IdField.Equals(value) != true)) {
this.IdField = value;
this.RaisePropertyChanged("Id");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string msg {
get {
return this.msgField;
}
set {
if ((object.ReferenceEquals(this.msgField, value) != true)) {
this.msgField = value;
this.RaisePropertyChanged("msg");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string name {
get {
return this.nameField;
}
set {
if ((object.ReferenceEquals(this.nameField, value) != true)) {
this.nameField = value;
this.RaisePropertyChanged("name");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged =
this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new
System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ChatService.Idatabase",
CallbackContract=typeof(WindowsFormsApp12.ChatService.IdatabaseCallback))]
public interface Idatabase {
[System.ServiceModel.OperationContractAttribute(Action="http://temp
uri.org/Idatabase/dataconnect",
ReplyAction="http://tempuri.org/Idatabase/dataconnectResponse")]
void dataconnect();
[
.......
好的,我找到我的问题了 App.config 文件,基地址
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="ClassLibrary1.wcf_chating" behaviorConfiguration="BehConfig">
<endpoint contract="ClassLibrary1.Iwcf_chating" binding="wsDualHttpBinding"
address=""/>
<endpoint contract="ClassLibrary1.Idatabase" binding="wsDualHttpBinding"
address=""/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:4000"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="BehConfig" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>