WCF通用字典和理解WCF [英] WCF Generic Dictionary and Understanding WCF
问题描述
好吧,我有大量的困难解决一个具体问题。一个对象通过服务转移。从概念上讲,这是有道理的......我觉得呢?从我读过,通用的无法改变,除非它已被明确定义连载
Okay, I'm having a large amount of difficulty solving a particular issue. The transfer of an object through the service. Conceptually, it makes sense... I think? From what I've read, a Generic can't be serialized unless it has been explicitly defined.
所以我想提供我的例子;在这我不能在所有的工作。意思是;我敢肯定有其他人谁遇到一些困难也是如此。当你帮助,如果你能提供的代码; ,将工作和解释。这样我可以完全理解这个问题。 。这将有助于我的好,了解Windows通讯基础
So I'd like to provide my example; in which I can't get to work at all. Which means; I'm sure there are others who experience some difficulty as well. When you assist if you could provide the code; that would work and explain it. That way I can fully understand the issue. Which will assist me on well, understanding the Windows Communication Foundation.
我们的目标是简单的有五个领域的客户端应用程序;其中邮报的服务器。
The goal is a client application which simply has five fields; in which it 'Post' to the server.
- 名字
- 姓
- 电子邮件地址
- 电话号码
- 网站地址
- First Name
- Last Name
- Email Address
- Phone Number
- Site Address
这是不是太复杂了。
下面是我做了什么,这在我的追求,学习WCF,我包括接近OOP校长,因为我可以为基于SOA的应用程序。
Here is what I've done, which in my quest to learn WCF I've included as close to OOP Principals as I can for a SOA based application. That way it provides code re-useability.
Model / Data Contract:
型号/数据合同$ C>#地区使用参考...
$ b $使用System.Runtime.Serialization b:
使用System.Collections.Generic;使用System.Threading.Tasks
;
使用System.Text;
使用System.Linq的;
使用系统;
#endregion
命名空间_2Do.Model.Customer
{
[DataContract(IsReference =真)]
公Person类
{
#区域声明的变量。
线第一;
串最后;
字符串电子邮件;
线电话;
串的网站;
#endregion
#地区的构造:
的人()
{
//空的构造。
}
#endregion
#地区的数据成员属性:
[数据成员()]
公共字符串首先
{
{返回第一; }
组第{=值; }
}
[数据成员()]
公共字符串最后
{
{返回最后; }
组最后{=价值; }
}
[数据成员()]
公共字符串的电子邮件
{
{返回电子邮件; }
集合{电子邮件=价值; }
}
[数据成员()]
公共字符串电话
{
{返回电话; }
集合{手机=价值; }
}
[数据成员()]
公共字符串网站
{
{返回网站; }
集合{网站=价值; }
}
#endregion
}
}
#region Using Reference...
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System;
#endregion
namespace _2Do.Model.Customer
{
[DataContract(IsReference = true)]
public class Person
{
#region Declared Variable.
string first;
string last;
string email;
string phone;
string site;
#endregion
#region Constructor:
Person()
{
// Empty Constructor.
}
#endregion
#region Data Member Properties:
[DataMember()]
public string First
{
get { return first; }
set { first = value; }
}
[DataMember()]
public string Last
{
get { return last; }
set { last = value; }
}
[DataMember()]
public string Email
{
get { return email; }
set { email = value; }
}
[DataMember()]
public string Phone
{
get { return phone; }
set { phone = value; }
}
[DataMember()]
public string Site
{
get { return site; }
set { site = value; }
}
#endregion
}
}
所以这是在其中应通过元数据客户端被暴露的对象;因此对于服务接口我试图这样的:
So that is the object in which should be exposed through the Metadata for the client; so for the Service interface I attempted this:
#region Using Reference...
using System.Collections.Generic;
using System.Threading.Tasks;
using System.ServiceModel;
using _2Do.Model.Customer;
using System.Text;
using System.Linq;
using System;
#endregion
namespace _2Do.Contract.Customer
{
[ServiceContract (Namespace = "https://_2Do") ]
public interface IPerson
{
[OperationContract()]
Person SetCustomer(Dictionary<Guid, Person> info);
}
}
所以上面是我们的目标;把我的Person对象;存入字典。另外要注意的;是我想通过引用传递的值将有助于序列化的实施。我想,一旦数据被存储在存储器中;它将包含一个明确的方法,为它来处理。那是错上我的一部分?
So the above is the goal; to transfer my Person object; stored into a Dictionary. The other thing to note; is I thought the implementation of passing the values by reference would assist in the serialization. I figured once the data is stored in memory; it would contain an explicit methodology for it to handle. Is that wrong on my part?
所以这是我的 DataContract
和的ServiceContract 。code>
在这一点的实现是这样的:
The implementation at this point is like this:
#region Using Reference...
using System.Collections.Generic;
using System.Threading.Tasks;
using _2Do.Contract.Customer;
using System.ServiceModel;
using _2Do.Model.Customer;
using System.Text;
using System.Linq;
using System;
#endregion
namespace _2Do.Service.Customer
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class PersonService : IPerson
{
#region Constructor:
PersonService()
{
// Empty Constructor.
}
#endregion
#region Implement Interface:
Person SetCustomer(Dictionary<Guid, Person> info)
{
// Receive an error that indicates; best overload method.
// Contains invalid arguments.
}
#endregion
}
}
然后我创建了一个单独的项目来承载的应用;我创建了一个空的文本文件,并将其重命名为 PersonService.svc
。
Then I created a separate project to host the application; which I created an empty text file and renamed it to PersonService.svc
.
然后我把:<%@ ServiceHost的服务=_2Do.Service.Customer.PersonService%GT;
。
这应该指向正确的命名空间;这在包含裸露的最低配置Internet信息系统主办的 PersonService
我有一个的web.config
文件。我本以为这让我绕过定义我的地址,绑定和契约。作为IIS会做这一切,我现在
Which should point to the proper namespace; which In the PersonService
I have a web.config
file that contains the bare minimum configuration to host in Internet Information System. Which I thought would allow me to circumvent defining my Address, Binding, and Contract. As IIS will do it all for me now.
然后我创建了一个 ClientProxy
类;其中包含镜子几乎 DataContract
。然后,我创建了实际的客户端应用程序做的:
Then I've created a ClientProxy
class; which contains a mirror almost of the DataContract
. Then I've created the actual client application to do:
PersonProxy p = new PersonProxy();
p.First = txtFirst.Text;
p.Last = txtLast.Text;
p.Email = txtEmail.Text;
p.Phone = txtPhone.Text;
p.Site = txtSite.Text;
Dictionary<Guid, Person> i = new Dictionary<Guid, Person>();
i.Add(Guid.NewGuid(), p);
我已经得到了这些项目:
I've got these projects:
- 模式 - > DataContract
- 合同 - >的ServiceContract
- 服务 - >为实现接口李>
- 主机 - >店铺服务/ SVC
- ClientProxy - >执行情况接口
- 客户端 - >链接为ClientProxy实际值继承。
- Model --> DataContract
- Contract --> ServiceContract
- Service --> Implementation for Interfaces
- Host --> Stores Service / Svc
- ClientProxy --> Implementation for Interface
- Client --> Linked to actual values for ClientProxy to inherit.
这就是我如何使用它的客户端。我不知道,我已经搞砸了这一点。我真的想了解WCF,因为我需要学习它一个工作项目。不过,我很沮丧,觉得很愚蠢,因为我似乎无法来解决这个问题。
Which is how I consume it on the Client. I'm not sure where I've messed up this. I'd really like to understand WCF as I need to learn it for a work project. But I'm so frustrated and feel so stupid as I can't seem to solve this issue.
如果我按照教程,它的工作原理。但是,一旦我回到我原来的执行失败。不知道是什么,或我要去哪里错了。有些手拿着,将不胜感激;但如果你能解释它一步一步它会如此赞赏。因此,我可以从我的错误中真正改善学习。
If I follow tutorials, it works. But once I go back to my original implementation it fails. Not sure what or where I'm going wrong. Some hand holding would be appreciated; but if you could explain it step by step it'd be so appreciated. So I can learn from my mistakes to actually improve.
我无法得到它的变量存储在服务器上,传送和我在这个不知道为什么点。
I can't get it to store the variable on the server, transmit, and I have no clue why at this point.
推荐答案
我仍然不知道为什么你的代码不能正常工作。也许我不明白,在IIS托管,以及我应该。然而,下面是最简单的Web服务+端,我可以拿出一个完全工作的样品。希望它会帮助您找到并解决问题。
I'm still not sure why your code is not working. Perhaps I don't understand hosting in IIS as well as I should. However, below is a fully working sample of the simplest web service + client that I can come up with. Hopefully it will help you find and address the issue.
首先,的项目结构:合同在一个类库项目,服务和主机在一个控制台应用程序,客户端在不同的控制台应用程序
First of all, project structure: contracts in one class library project, service and host in a console app, client in a different console app.
数据合同:
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
服务合同:
Service contract:
[ServiceContract]
public interface IPerson
{
[OperationContract]
Person SetCustomer(Dictionary<Guid, Person> info);
}
服务:
Service:
public class PersonService : IPerson
{
public Person SetCustomer(Dictionary<Guid, Person> info)
{
foreach (var person in info.Values)
{
Console.WriteLine("Name: {0} | Email: {1}", person.Name, person.Email);
}
var p = new Person { Name = "John Doe", Email = "John@Doe.com" };
return p;
}
}
服务主机(在主机项目的Program.cs中)
Service Host (in the host project's Program.cs):
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(PersonService), new Uri("http://localhost:8080")))
{
host.Open();
Console.WriteLine("Ready!");
Console.ReadKey(true);
host.Close();
}
}
最后,客户端(在自己的项目的计划。 CS):
And finally, client (in its own project's Program.cs):
static void Main(string[] args)
{
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost:8080/");
using (var factory = new ChannelFactory<IPerson>(binding, endpoint))
{
var request = new Dictionary<Guid, Person>();
request[Guid.NewGuid()] = new Person { Name = "Bob", Email = "Bob@abc.com" };
var client = factory.CreateChannel();
var result = client.SetCustomer(request);
Console.WriteLine("Name: {0} | Email: {1}", result.Name, result.Email);
factory.Close();
}
Console.ReadKey(true);
}
希望这有助于!
Hope this helps!
这篇关于WCF通用字典和理解WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!