C# 客户端代理未创建方法
我有一个错误:C# 客户端代理未创建方法,c#,linq,wcf,visual-studio-2013,C#,Linq,Wcf,Visual Studio 2013,我有一个错误:无法将类型“void”隐式转换为“string”。我按照每个步骤正确创建Web服务(首先编写服务,编译并在浏览器中查看它,然后使用“添加服务引用”向导创建服务引用)。以下是WP代码: private void Button_Click(object sender, RoutedEventArgs e) { ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Clien
无法将类型“void”隐式转换为“string”
。我按照每个步骤正确创建Web服务(首先编写服务,编译并在浏览器中查看它,然后使用“添加服务引用”向导创建服务引用)。以下是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) {
}
它应该是空的吗?这个问题有什么解决办法吗?编辑:以橙色供电。我的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>
我不确定你所说的“服务实现对象”是什么意思(对不起,我是初学者),但我在Reference.cs中找到了这个(如果我发布了错误的代码,请更正我)
编辑:附加参考.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);
}
由于您使用的是基于事件的异步形式的操作,因此需要如下内容:
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];
}
引用是从契约生成的,契约是您在某处定义的接口。更具体地说,
PhoneApp1.ServiceReference1.IService1
有一个名为GetAge
的无效方法。您需要更改接口上的签名并重新生成代理。@PoweredByOrange,我只有字符串GetAge(字符串myName)[OperationContract]
下的code>。我从头开始这个项目,只添加了这个方法。这是我现在的签名吗?奇怪。您是如何生成代理的?@PoweredByOrange,我正在使用Visual Studio的“添加服务引用”对话框。是的,但是您在URL中键入了什么?我遇到了一个错误:找不到类型或命名空间名称“GetAgeCompletedEventArgs”(您是否缺少using指令或程序集引用?)
我打赌你可以自己解决这个问题,但我打赌这确实是服务引用1.GetAgeCompletedEventArgs
。这很有帮助,但出现了另一个错误:无法将类型“char”转换为“string”
。我像AgeTxtBlck.Text=e.Result那样修复了它代码>。啊哈!我想你遗漏了上面的一些代码。属性Result
可能被定义为get{return(string)this.Result[0];}
,这样就不需要我的强制转换和[0]
。
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;
}
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);
}
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];
}