首页 > 技术文章 > Silverlight WCF RIA service Not Found

wangn 2013-07-24 13:05 原文

 

 

完整记录新建一个简单的silverlight+wcf ria services的过程。

 

STEP 1:新建一个silverlight应用程序

STEP 2:新建一个wcf ria servies class library

image

生成的RIAServicesLibrary2.Web这种名称的类库就是所谓的服务端,RIAServicesLibrary2就是所谓的客户端。

STEP 3 添加服务

[EnableClientAccess()]
    public class DomainService2 : DomainService
    {
        //[Invoke]
        public string Test()
        {
            return "aaa";
        }
        //[Invoke]
        public string GetData(int id)
        {
            //Sample sp = new Sample();
            //return sp.SelectData(id);
            return "Hello World";
        }
    }

在查阅资料的时候有同学说方法上要加[Invoke],经测试不加也是可以的。

STEP 4 配置

A web.config

<configuration>
  <system.web>
    <httpModules>
      <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpModules>

    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"></customErrors>
  </system.web>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

</configuration>

其中<httpModules>和 <system.serviceModel>经我测试是必须要有的,否则会报如下错误。
{System.Windows.ApplicationUnhandledExceptionEventArgs}
    base {System.EventArgs}: {System.Windows.ApplicationUnhandledExceptionEventArgs}
    ExceptionObject: {System.ServiceModel.DomainServices.Client.DomainOperationException: Invoke operation 'GetData' failed. 远程服务器返回了错误: NotFound。 ---> System.ServiceModel.CommunicationException: 远程服务器返回了错误: NotFound。 ---> System.Net.WebException: 远程服务器返回了错误: NotFound。 ---> System.Net.WebException: 远程服务器返回了错误: NotFound。

B 需要将RIAServicesLibrary2和RIAServicesLibrary2.Web中生成的.dll文件拷至 网站根目录下的bin文件夹内。

 


 

STEP 5 测试

InvokeOperation<string> o = null;

RIAServicesLibrary2.Web.DomainService2 s = new RIAServicesLibrary2.Web.DomainService2();
o = s.GetData(1, Callback, "abc");
s.Test(Callback, "111");

void Callback(InvokeOperation<string> io)
      {
          try
          {
              if (io.UserState.ToString() == "abc")
                  this.tb1.Text = io.Value;
              else
                  this.tb2.Text = io.Value;
              Console.WriteLine(io.Value);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex);
          }

      }

InvokeOperation<string>类型的对象有Completed事件,不过个人建议使用上述写法。

 

过程中遇到的问题与总结:

   1、少做了第4步,一直报异常如下。

{System.Windows.ApplicationUnhandledExceptionEventArgs}
base {System.EventArgs}: {System.Windows.ApplicationUnhandledExceptionEventArgs}
ExceptionObject: {System.ServiceModel.DomainServices.Client.DomainOperationException: Invoke operation 'GetData' failed. 远程服务器返回了错误: NotFound。 ---> System.ServiceModel.CommunicationException: 远程服务器返回了错误: NotFound。 ---> System.Net.WebException: 远程服务器返回了错误: NotFound。 ---> System.Net.WebException: 远程服务器返回了错误: NotFound。

 参考资料:http://blog.csdn.net/hawksoft/article/details/6762267

http://blogs.msdn.com/b/webapps/archive/2013/01/11/how-to-resolve-not-found-error-in-silverlight-for-wcf-ria-services-calls.aspx

http://msdn.microsoft.com/en-us/library/ff426913(v=VS.91).aspx

 代码

 

推荐阅读