客户端代理不创建方法

时间:2014-06-13 20:17:35

标签: c# linq wcf visual-studio-2013

我有一个错误:Cannot implicitly convert type 'void' to 'string'。我按照每一步都正确地创建了webservice(首先写了服务,编译它并在浏览器中查看它。然后使用&#34创建服务引用;添加服务引用"向导)。这是WP代码:

private void Button_Click(object sender, RoutedEventArgs e)
    {
         ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
        string myName = "Nick";
        AgeTxtBlck.Text = proxy.GetAgeAsync(myName);
    }

服务实施是:

public string GetAge(string myName)
    {
        DataClasses1DataContext data = new DataClasses1DataContext();
        var myAge = from a in data.Users where a.uName == myName select a;
        return GetAge(myName).ToString();
    }

为了使我的问题更清楚,这里是客户端生成的代理:

 public partial class Service1Client : System.ServiceModel.ClientBase<PhoneApp1.ServiceReference1.IService1>, PhoneApp1.ServiceReference1.IService1 {

    private BeginOperationDelegate onBeginGetAgeDelegate;
    private EndOperationDelegate onEndGetAgeDelegate;
    private System.Threading.SendOrPostCallback onGetAgeCompletedDelegate;
    private BeginOperationDelegate onBeginOpenDelegate;
    private EndOperationDelegate onEndOpenDelegate;
    private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
    private BeginOperationDelegate onBeginCloseDelegate;
    private EndOperationDelegate onEndCloseDelegate;

    private System.Threading.SendOrPostCallback onCloseCompletedDelegate;

    public Service1Client() {
    }

    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

此外,我不知道它是否与问题相关,但在Reference.cs文件中我发现了这一点:

 public void GetAgeAsync(string myName) {
        this.GetAgeAsync(myName, null);
    }

它应该是无效的吗?有没有解决这个问题的方法?
编辑:To PoweredByOrange。我的Web.config是:

 <?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <add name="Database1ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

我的合同是:

    namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
         string GetAge(string myName);
    }
}

而且我不确定&#34;服务实现对象&#34;是什么意思? (对不起,我是初学者),但我在Reference.cs中找到了这个(如果我发错了代码,请更正我)

public partial class GetAgeCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
    private object[] results;
    public GetAgeCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
            base(exception, cancelled, userState) {
        this.results = results;
    }

编辑:附加Reference.cs代码:

public void GetAgeAsync(string myName, object userState) {
        if ((this.onBeginGetAgeDelegate == null)) {
            this.onBeginGetAgeDelegate = new BeginOperationDelegate(this.OnBeginGetAge);
        }
        if ((this.onEndGetAgeDelegate == null)) {
            this.onEndGetAgeDelegate = new EndOperationDelegate(this.OnEndGetAge);
        }
        if ((this.onGetAgeCompletedDelegate == null)) {
            this.onGetAgeCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetAgeCompleted);
        }
        base.InvokeAsync(this.onBeginGetAgeDelegate, new object[] {
                    myName}, this.onEndGetAgeDelegate, this.onGetAgeCompletedDelegate, userState);
    }

1 个答案:

答案 0 :(得分:1)

由于您使用的是基于事件的异步操作形式,因此您需要这样的内容:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
    proxy.GetAgeAsyncCompleted += SetAgeText;
    string myName = "Nick";
    proxy.GetAgeAsync(myName);
}

private void SetAgeText(object sender, ServiceReference1.GetAgeCompletedEventArgs e)
{
    AgeTxtBlck.Text = (string) e.Result[0];
}