Silverlight+WCF双工问题,菜鸟求助,急
本帖最后由 alian_1126 于 2011-03-13 10:56:49 编辑 我在Silverlight项目里添加了WCF服务引用,在Page.xaml.cs里用Service References智能提示可以弹出WCF服务接口IXMLService和IXMLServiceClient,却弹不出回调接口IXMLServiceCallBack,为什么啊,我要在SL里实现这个回调啊,弹不出来怎么实现啊,刚学,遇到问题了,前辈们帮帮我,谢谢
以下是WCF里的代码
//契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
namespace WcfService
{
[ServiceContract(CallbackContract = typeof(IXMLClient))]
public interface IXMLService//服务接口
{
[OperationContract(IsOneWay = true)]
void GetData(Uri uri);
}
public interface IXMLClient//回调接口
{
[OperationContract(IsOneWay = true)]
void BackToClient(String xmlData);
}
}
//服务
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace WcfService
{
public class Service:IXMLService
{
IXMLClient client = OperationContext.Current.GetCallbackChannel<IXMLClient>();
public void GetData(Uri uri)
{
WebClient webclient = new WebClient();
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
webclient.DownloadStringAsync(uri);
}
void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//throw new NotImplementedException();
if (null == e.Error) { return; }
client.BackToClient(e.Result);
}
}
}
[其他解释]
wsDualHttpBinding,寄宿在IIS中?
[其他解释]
哦,我刚才测试了一下,原先我们有一个WCF服务是net.tcp的,以前都是通过svcutil.exe来生成代理类。
刚才我测试了一下,新建silverlight工程,然后添加服务引用net.tcp://xxx.xxx.xxx.xxx:8001/wcfservice,能生成回调方法。
你这个改为下面的试试看行不行
接口那里添加SessionMode = SessionMode.Required
服务那里添加[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
[其他解释]
ServiceReferences.IXMLClientCallback -> ServiceReferences.IXMLClient
服务端也没有定义叫 IXMLClientCallback 的接口
[其他解释]
搜索了半天,才找到net.tcp不适合公网,WSDualHttpBinding适合公网这种说法的来源。
http://social.msdn.microsoft.com/Forums/zh-CN/wcfzhchs/thread/6db85086-a121-460a-9f1d-06921fb93166
这里这位大牛说的是吧?多数谬论就是这样一传十十传百出来的,我不太相信如果我手里有一块煤炭是黑的,人们在论坛上就可以把它生生说成白的。
楼主的问题是不能在引用时生成回调方法,[ServiceContract(CallbackContract = typeof(IXMLClient))]本身就为service定义了回调接口,还何必显式指定。引用时不能得到回调方法和方法具体实现没有关系。
[其他解释]
不行的话,服务端契约,明确定义下名字也行。
[ServiceContract(Name="IXMLClientCallback")]
public interface IXMLClient//回调接口
{
[OperationContract(IsOneWay = true)]
void BackToClient(String xmlData);
}
[其他解释]
SL 的 WCF 使用果然有些不同,看看这篇文章:
http://www.cnblogs.com/li_g_7711/archive/2009/11/02/1594584.html
[其他解释]
来人帮帮忙啊
[其他解释]
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,
还可以使用 Web 管理工具来配置应用程序的设置。
可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" ,将调试符号
插入已编译的页面中。但由于这会影响性能,
因此请只在开发过程中
将此值设置为 true。
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<!--
通过 <authentication> 节可以配置
ASP.NET 使用的安全身份验证
模式,以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
通过 <customErrors> 节可以配置在执行请求过程中出现未处理错误时,
应执行的操作。
具体说来,开发人员通过该节
可以配置要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
在 Internet 信息服务 7.0 下,运行 ASP.NET AJAX 要求
system.webServer 节。这在以前版本的 IIS 中并非必需。
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="WcfService.ServiceBehavior" name="WcfService.Service">
<endpoint address="" binding="wsDualHttpBinding" contract="WcfService.IXMLService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/XMLService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService.ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<bindings />
<client />
</system.serviceModel>
</configuration>
public class ClientCallback: ServiceReferences.IXMLClientCallback//这里点不出来这个回调契约让我实现啊
{
//...
}
InstanceContext instanceContext = new InstanceContext(new ClientCallback());
ServiceReference.XMLServiceClient = new ServiceReference.XMLServiceClient(instanceContext);