WCF问题 [英] WCF Question

查看:56
本文介绍了WCF问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<> WCF问题


大家好,


Ia ??对于WCF来说很新,并且有一个关于它是如何工作的基本问题。 Ia ?? m

试图使用WCF写一个SOA系统??并且遇到一些问题。


以下类比解释了我想做什么。我有一个非常简单的

课程:


[DataContract]

公共课MyClass

{

私有字符串数据="默认数据";


[DataMember]

公共字符串数据

{

get

{

返回数据;

}

set

{

data = value;

}

}

}


这是通过WCF服务公开的:


[ServiceContract]

公共接口IMyClassService

{

[OperationContract]

void添加(MyClass项目);

}


public class MyClassService:IMyClassService

{

public void Add(MyClass item)

{

//做点什么数据......

Debug.WriteLine(item.Data);

}

}


如您所见,这非常简单。我把服务放在了一个ASP.NET

的web应用程序中,创建了正确的web-config条目,包括一个带有正确标签的svc文件

,以及服务确实有效。我通过控制台应用程序(通过创建服务引用)消耗了服务

。我在控制台应用程序中有以下(非常简单)代码的




公共静态类程序

{

public static void Main(string [] args)

{

MyClassServiceClient client = new MyClassServiceClient();


MyClass myClass = new MyClass();


client.Add(myClass);

}

}


显然,控制台应用程序中的MyClass引用是WCF自动生成的代理类

。它只知道带有[DataMember]属性标记为

的属性。这意味着任何一个私有?

中的代码控制台应用程序不执行MyClass。但是,当代理

传递给服务时,我会期待a ?? reala的实例?或者完整的

要在WCF服务器上实例化的MyClass类,我希望

私有代码在构造时运行。它确实没有!这可以通过查看服务器上Data属性的值来证明

。它仍设置为

null,尽管在字段声明中给它一个值。


因此,如果MyClass有一个带代码的构造函数,这不会在

服务器上点火。


所以,我的问题是这样的:有没有办法确保构建MyClass

完全在服务器上?如果构造函数代码可以触发
,那将是很好的。可以通过调整

服务类中的属性来获得这种行为吗?


提前致谢,


史蒂夫。

解决方案



我不知道这是否有帮助。


但这是一个非代理类的方法,在DotNet到DotNet的世界里,

WCF。


http ://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!158.entry


" Steve Barker" < st ********** @ nospam.nospamwrote in message

news:88 ******************** ************** @ microsof t.com ...


WCF问题


大家好,


我对WCF很新,并且有一个关于它是如何工作的基本问题。我是b $ b试图使用WCF编写SOA系统,并遇到一些问题。


以下类比解释了什么我正在努力做到。我有一个非常简单的

课程:


[DataContract]

公共课MyClass

{

私有字符串数据="默认数据";


[DataMember]

公共字符串数据

{

get

{

返回数据;

}

set

{

data = value;

}

}

}


这是通过WCF服务公开的:


[ServiceContract]

公共接口IMyClassService

{

[OperationContract]

void添加(MyClass项目);

}


public class MyClassService:IMyClassService

{

public void Add(MyClass item)

{

//做点什么与数据......

Debug.WriteLine(item.Data);

}

}


尽可能看,这非常简单。我把服务放在了一个

ASP.NET

的web应用程序中,创建了正确的web-config条目,包括一个svc

文件
带有正确标签的
,该服务确实有效。我通过控制台应用程序(通过创建服务引用)使用了服务

。我在控制台应用程序中有以下(非常简单)代码的




公共静态类程序

{

public static void Main(string [] args)

{

MyClassServiceClient client = new MyClassServiceClient();


MyClass myClass = new MyClass();


client.Add(myClass);

}

}


显然,控制台应用程序中的MyClass引用是WCF自动生成的代理



。它只知道带有[DataMember]属性标记为

的属性。这意味着任何私人的

中的代码控制台应用程序不执行MyClass。但是,当

代理

传递给服务时,我会期待一个真实的实例。或者完整的

要在WCF服务器上实例化的MyClass类,我希望

私有代码在构造时运行。它不是!这可以通过查看服务器上Data属性的值来证明

。尽管在字段声明中给它一个值,它仍然设置



null。


因此,如果MyClass有一个带代码的构造函数,这不会在

服务器上触发。


所以,我的问题是:有没有办法确保MyClass

完全在服务器上构建?如果构造函数代码

可能会被解雇,那将是很好的。可以通过调整

服务类中的属性来获得这种行为吗?


提前致谢,


史蒂夫。



看起来这里的问题是你确实发送了一个< nullup

wire,因为你实例化了:


MyClass myClass = new MyClass();

client.Add(myClass);


因为代理MyClass不知道更好,所有字段都是

初始化为默认值(字符串为null)并主动发送。这个

(在服务器上)基本上(对于真实对象):


MyClass serverObj = new MyClass();

serverObj.Data = null;

//等


您需要告诉班级有关默认值...在这种情况下,您可以这样做

by(在服务器对象上):


私有字符串数据="默认数据" ;;


[ DataMember,DefaultValue(默认数据)]

公共字符串数据{...}


(尽管你可能需要重新生成客户代理

更新它。


这[DefaultValue]是合同的一部分,所以现在客户代理

将a:初始化为默认数据,并且b:不打扰发送默认

数据,因为它可以在服务器上假设(您可以使其发送默认值
通过调整可选的[DataMember]属性来
值。


Marc


嗯,你不能阻止客户做它喜欢的事情......按照定义

代理没有任何逻辑。你也不能拿走默认的ctor,

或者序列化器会中断。


所以你*可以*有一个New()方法服务,但似乎

矫枉过正。您有什么样的逻辑?


或者,您可以在

客户端的部分类中创建一个方法(在单独的文件中为创建

a New()方法的服务,但是相同的类型。您还可以检查WCF生成的代理是否公开任何可能有帮助的部分方法(如果您使用的是C#3)。


如果您正在做.NET-to-.NET,请注意,您不必自己限制代理人使用
;您还可以在

客户端和服务器之间共享.cs(或包含您的数据合同/服务合同的整个

assemlby);这允许你在每个都运行相同的代码,但

请注意,服务器不应该假设任何这样的逻辑已经发生

(因为敌对客户端可能是发送任何旧垃圾)。


请注意,除[DefaultValue]外,DataContractSerializer还支持bool ShouldSerialize {PropertyName}()。复杂的模式

可选值,但如果使用浅代理

,则不能在客户端工作。


Marc


WCF Question

Hi guys,

Ia??m pretty new to WCF, and have a basic question about how it works. Ia??m
trying to use WCF to write an a??SOA-systema??, and am having a few problems.

The following analogy explains what Ia??m trying to do. I have a very simple
class:

[DataContract]
public class MyClass
{
private string data = "default data";

[DataMember]
public string Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

This is exposed via a WCF Service:

[ServiceContract]
public interface IMyClassService
{
[OperationContract]
void Add(MyClass item);
}

public class MyClassService : IMyClassService
{
public void Add(MyClass item)
{
//Do something with the data...
Debug.WriteLine(item.Data);
}
}

As you can see, this is extremely simple. Ia??ve put the service in an ASP.NET
web application, created the correct web-config entries, included an svc file
with the correct tag, and the service does work. Ia??ve consumed the service
via a console application (by creating a service reference). I have the
following (very simple) code in the console application:

public static class Program
{
public static void Main(string[] args)
{
MyClassServiceClient client = new MyClassServiceClient();

MyClass myClass = new MyClass();

client.Add(myClass);
}
}

Obviously, the MyClass reference in the console application is a proxy class
generated automatically by WCF. It only knows about the properties marked
with [DataMember] attributes. This means that any a??privatea?? code within
MyClass is not executed by the console application. However, when the proxy
is passed to the service, I would expect an instance of the a??reala?? or full
MyClass class to be instantiated on the WCF server, and I would expect the
private code to run on construction. It doesna??t! This can be proved by
looking at the value of the Data property on the server. It is still set as
null, despite giving it a value in the field declaration.

Hence, if MyClass has a constructor with code, this would not fire on the
server.

So, my questions is this: Is there a way to ensure that MyClass is
constructed fully on the server? It would be great if constructor code could
be fired. Can this behaviour be obtained by tweaking the attributes in the
service classes?

Thanks in advance,

Steve.

解决方案


I don''t know if this helps or not.

But this is a non-proxy-class approach, in a DotNet to DotNet world with
WCF.

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!158.entry

"Steve Barker" <st**********@nospam.nospamwrote in message
news:88**********************************@microsof t.com...

WCF Question

Hi guys,

I''m pretty new to WCF, and have a basic question about how it works. I''m
trying to use WCF to write an "SOA-system", and am having a few problems.

The following analogy explains what I''m trying to do. I have a very simple
class:

[DataContract]
public class MyClass
{
private string data = "default data";

[DataMember]
public string Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

This is exposed via a WCF Service:

[ServiceContract]
public interface IMyClassService
{
[OperationContract]
void Add(MyClass item);
}

public class MyClassService : IMyClassService
{
public void Add(MyClass item)
{
//Do something with the data...
Debug.WriteLine(item.Data);
}
}

As you can see, this is extremely simple. I''ve put the service in an
ASP.NET
web application, created the correct web-config entries, included an svc
file
with the correct tag, and the service does work. I''ve consumed the service
via a console application (by creating a service reference). I have the
following (very simple) code in the console application:

public static class Program
{
public static void Main(string[] args)
{
MyClassServiceClient client = new MyClassServiceClient();

MyClass myClass = new MyClass();

client.Add(myClass);
}
}

Obviously, the MyClass reference in the console application is a proxy
class
generated automatically by WCF. It only knows about the properties marked
with [DataMember] attributes. This means that any "private" code within
MyClass is not executed by the console application. However, when the
proxy
is passed to the service, I would expect an instance of the "real" or full
MyClass class to be instantiated on the WCF server, and I would expect the
private code to run on construction. It doesn''t! This can be proved by
looking at the value of the Data property on the server. It is still set
as
null, despite giving it a value in the field declaration.

Hence, if MyClass has a constructor with code, this would not fire on the
server.

So, my questions is this: Is there a way to ensure that MyClass is
constructed fully on the server? It would be great if constructor code
could
be fired. Can this behaviour be obtained by tweaking the attributes in the
service classes?

Thanks in advance,

Steve.



It looks like the problem here is that you did actually send a <nullup
the wire, because you instantiated with:

MyClass myClass = new MyClass();
client.Add(myClass);

Because the proxy MyClass doesn''t know any better, all fields are
initialized to their defaults (null for string) and actively sent. This
(at the server) essentially does (for the real objects):

MyClass serverObj = new MyClass();
serverObj.Data = null;
// etc

You need to tell the class about defaults... in this case, you can do it
by (on the server object):

private string data = "default data";

[DataMember, DefaultValue("default data")]
public string Data {...}

(although you''ll probably need to regenerate the client proxies to
update it).

This [DefaultValue] is part of the contract, so now the client proxy
will a: initialize as "default data", and b: not bother sending "default
data", as it can be assumed at the server (you can make it send default
values by tweaking the optional [DataMember] properties).

Marc


Well, you can''t stop the client doing what it likes... and by definition
proxies don''t have any logic. You also can''t take away the default ctor,
or the serializer will break.

So you *could* have a New() method on the service, but it seems
overkill. What sort of logic did you have in mind?

Alternatively, you could create a method in a partial class at the
client (in a separate file for the service, but same types) that creates
a New() method. You could also check whether the WCF-generated proxy
exposes any partial methods that might help (if you are using C# 3).

If you are doing .NET-to-.NET, note that you don''t have to limit
yourself to the proxies; you can also share the .cs (or the entire
assemlby containing your data-contracts / service-contracts) between the
client and the server; this allows you to run the same code at each, but
note that the server shouldn''t assume that any such logic has occurred
(since a hostile client could be sending any old rubbish).

Note that in addition to [DefaultValue], DataContractSerializer also
supports the "bool ShouldSerialize{PropertyName}()" pattern for complex
optional values, but this won''t work at the client if shallow proxies
are used.

Marc


这篇关于WCF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆