silverlight正在调用WCF服务但不传递参数

时间:2013-12-17 12:30:57

标签: c# asp.net wcf silverlight

我在项目中有一个WCF服务,具有以下设置

//接口

namespace AttendanceSystem
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void DoWork(string s);
    }
}

//以及以下实现

namespace AttendanceSystem
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public void DoWork(string s)
        {
            StreamWriter file = new StreamWriter(@"C:\users\waqasjafri\desktop\test.txt");
            if (s == null)
            {
                file.WriteLine("value is null");
            }
            else
            {
                file.WriteLine("value is not null");
            }
            file.Close();
        }
    }
}

以下是我的解决方案的网站应用程序部分中与wcf服务有关的web.config文件

  <system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

和silverlight项目中的ServiceReference.clientconfig文件

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:48886/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

//这里是调用代码......它是在点击按钮时触发的事件

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
obj.DoWorkAsync("Test");

该参数始终作为null传递。出了什么问题?由于我不熟悉WCF / silverlight / Asp.net,我无法理解。

2 个答案:

答案 0 :(得分:0)

再次更新您的服务参考。试试这样,在silverlight中你的方法应该有一个完成的事件,

  ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.DoWorkCompleted += (a, ae) =>
            { 

            };
            client.DoWorkAsync("Test");

答案 1 :(得分:0)

您可能超出了maxStringContentLength,默认情况下为8192。见这里:

http://msdn.microsoft.com/en-us/library/ms731361(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/ms731325(v=vs.110).aspx

您可能还想考虑使用二进制消息编码。