使用相同的外部错误的 WCF 会引发错误



嗨,我正在创建WCF服务,我已经通过创建类库项目创建了三个(问题,答案和类别)类,并使用它们的DLL创建服务,如下面的代码所示

public Answers InsertAnswer(Answers answer)
{
try
{
answer_ = new Answers();
cs = ConfigurationManager.ConnectionStrings[csName].ConnectionString;
using (con = new SqlConnection(cs))
{
cmd = new SqlCommand("InsertAnswer", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@answer", answer.Answer);
cmd.Parameters.AddWithValue("@questionId", answer.QuestionId);
con.Open();
cmd.ExecuteNonQuery();
answer_ = GetAnswer(answer);
}
}
catch (Exception ex)
{
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}
return answer_;
}

answer_ 是类答案的全局对象,位于

using QuestionsAnswers;

命名空间现在我在我的客户端使用相同的命名空间并使用相同的服务来调用该方法,但给了我错误 下面是我的客户端的代码

public void InserQuestionAnswer(QA qaParam)
{
try
{
Answers answer = new Answers();
answer.QuestionId = 1;
answer.Answer = qaParam.Answer.ToString();
QuestionsAnswersService.QuestionAnswerServiceClient questionAnswerService = new QuestionsAnswersService.QuestionAnswerServiceClient("BasicHttpBinding_IQuestionAnswerService");
questionAnswerService.InsertAnswer(answer);
}
catch (Exception ex)
{
}
}

我使用相同的引用

using QuestionsAnswers;

我收到以下错误

Severity    Code    Description Project File    Line    Suppression State
Error   CS0120  An object reference is required for the non-static field, 
method, or property 'QAaspx.InserQuestionAnswer(QA)'    QASamapleUI E:Rohit 
GuptaPracticeProjectsWebAppQASamapleUIQASamapleUIQAaspx.aspx.cs    25  
Active

任何人都可以帮助我吗

您收到此消息是因为您使用QAaspx.InserQuestionAnswer(QA)调用的方法不是静态的,并且显然您在调用它时没有对其类的对象引用。

可以通过创建该类的实例并调用该方法来解决此问题。您不会在代码中显示它在哪个类中,但让我们假设该类是 Foo。然后你可以这样做:

Foo myFoo = new Foo();
myFoo.InserQuestionAnswer(QA);

或者,如果该方法不需要引用其类的任何其他属性,则可以将其设置为静态。

public static void InserQuestionAnswer(QA qaParam) {} 

相关内容

最新更新