[英]Problems with the transfer of custom class in WCF c#
我正在服務器和客戶端之間傳輸數據。 客戶端很容易從服務器接收常規數據類型,例如int
,但是如果將自定義類傳遞給它,那么我將收到以下異常:
System.ServiceModel.CommunicationException:獲取HTTP響應http://127.0.0.1:8000/Service時發生錯誤。 這可能是由於服務端點綁定未使用HTTP協議這一事實造成的。 這也可能是由於HTTP請求上下文已被服務器中斷(可能是由於服務被禁用)所致。 有關詳細信息,請參見服務器日志。
我嘗試了許多解決方案來解決此類問題,但沒有任何幫助。 我在類字段中添加了getter和setter方法,將DataContract
等更改為Serializable
:
自定義類
namespace c_CardStrategy
{
[DataContract]
public class Card
{
#region ПОЛЯ
[DataMember]
public string Name { get; set; }
[DataMember]
public string Effect { get; set; }
[DataMember]
public int ID { get; set; }
[DataMember]
public int Price { get; set; }
[DataMember]
public string EffText { get; set; }
#endregion
#region Конструктор
public Card(int id, string name, string effect, int price, string efftext)
{
Price = price;
ID = id;
Name = name;
Effect = effect;
EffText = efftext;
}
public Card()
{
Name = "";
Effect = "";
ID = 0;
Price = 0;
EffText = "";
}
#endregion
}
[DataContract]
public class Moster_Card : Card
{
[DataMember]
public int? Health { get; set; }
[DataMember]
public int? Power { get; set; }
public Moster_Card(int id, string name, int? health, int? power, string effect, int price, string efftext) : base(id, name, effect, price, efftext)
{
Health = health;
Power = power;
}
public Moster_Card()
{
Name = "";
Effect = "";
ID = 0;
Price = 0;
EffText = "";
Health = 0;
Power = 0;
}
}
[DataContract]
public class Spell_Card : Card
{
[DataMember]
public int? Charge { get; set; }
public Spell_Card(int id, string name, string effect, int price, string efftext, int? charge) : base(id, name, effect, price, efftext)
{
Charge = charge;
}
public Spell_Card()
{
Name = "";
Effect = "";
ID = 0;
Price = 0;
EffText = "";
Charge = 0;
}
}
}
接口
[ServiceContract]
public interface IGame
{
[OperationContract]
Players GetPlayer(int grid);
[OperationContract]
List<Players> Players();
[OperationContract]
int GetTurn(int GameGrid);
[OperationContract]
void FindGame(Players Player);
[OperationContract]
object GetAllCards();
[OperationContract]
List<Card> GetDeckOnCode(string code_deck);
[OperationContract]
bool Checkuser(string login, string pass);
[OperationContract]
void WriteInLog(string Text);
}
服務器
namespace c_CardStrategy
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class GameServer : IGame
{
private List<Players> listPlayers;
private List<Game> Games;
public hpartner_cgEntities context;
...
public object GetAllCards()
{
context = new hpartner_cgEntities();
List<Card> Cards = new List<Card>();
foreach(var Card in context.Cards)
{
int id = Card.Card_id;
string name = Card.Card_name;
int f = Card.Card_eff_id;
string eff;
switch (f)
{
case 0:
eff = "None";
break;
case 1:
eff = "Taunt";
break;
case 2:
eff = "Charge";
break;
default:
eff = "None";
break;
}
int price = Card.Card_price;
string efftext = Card.Card_eff_text;
int? Health = Card.Card_health;
int? Power = Card.Card_power;
int? Charge = Card.Charge;
if (Health != null)
{
Cards.Add(new Spell_Card(id, name, eff, price, efftext, Charge));
}
else
{
Cards.Add(new Moster_Card(id, name, Health, Power, eff, price, efftext));
}
}
return Cards;
}
...
}
}
客戶
public partial class DecksWindow : Window
{
private GameClient Client;
public DecksWindow(GameClient client)
{
InitializeComponent();
Client = client;
}
private void New_deck_Click(object sender, RoutedEventArgs e)
{
Deck_list.Visibility = Visibility.Visible;
List<Card> Cards = new List<Card>();
try
{
Cards = Client.GetAllCards() as List<Card>;
foreach (Card Card in Cards)
{
StackPanel Sp = new StackPanel();
Sp.Height = 170;
Sp.Width = 150;
Sp.Margin = new Thickness(0, 5, 5, 0);
Sp.Background = Brushes.Red;
Sp.Tag = Card.IDk__BackingField.ToString();
Sp.Uid = 2.ToString();
Deck_list.Children.Add(Sp);
}
}
catch (Exception ex)
{
tb.Text += ex.Message;
}
//tb.Text = Client.Checkuser("Admin", "Admin").ToString();
}
}
配置客戶端
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework" />
</providers>
</entityFramework>
<connectionStrings>
<add name="hpartner_cgEntities" providerName="System.Data.EntityClient" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=MySql.Data.MySqlClient;provider connection string="server=hpartnerlink.ru;user id=hpartner;password=M7gn46Wx3b;persistsecurityinfo=True;database=hpartner_cg"" />
</connectionStrings>
<system.web>
<httpRuntime maxRequestLength="262144" executionTimeout="103600"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IGame" />
<binding name="BasicHttpBinding_IGame1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1:8000/Service" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IGame" contract="GameServiceReference.IGame"
name="BasicHttpBinding_IGame" />
<endpoint address="http://0.0.0.0:8000/Service" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IGame1" contract="ServiceReference1.IGame"
name="BasicHttpBinding_IGame1" />
</client>
</system.serviceModel>
配置服務器
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework" />
</providers>
</entityFramework>
<connectionStrings>
<add name="hpartner_cgEntities" providerName="System.Data.EntityClient" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=MySql.Data.MySqlClient;provider connection string="server=hpartnerlink.ru;user id=hpartner;password=M7gn46Wx3b;persistsecurityinfo=True;database=hpartner_cg"" />
</connectionStrings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MyServiceName" behaviorConfiguration="debug" />
</services>
</system.serviceModel>
托管服務后可以訪問Web服務的WSDL嗎? 我懷疑配置可能太簡單了。 我建議您在服務器端應用以下配置。
<system.serviceModel>
<services>
<service name="YourNamespace.GameServer " behaviorConfiguration="mybehavior">
<endpoint address="http://localhost:8000/Service" binding="basicHttpBinding" contract="YourNamespace.IGame " ></endpoint>
<endpoint address="http://localhost:8000/Service/mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mybehavior">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
托管服務后,我們可以通過在瀏覽器地址欄中鍵入服務地址來訪問服務信息。
http://localhost:8000/service
此外,托管服務時,我們可能需要管理特權。 隨時讓我知道問題是否仍然存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.