C# 尝试访问库时WCF引发异常
我是一名试图建立WCF服务的学生。我让主机和客户端使用servicelibrary运行,它工作正常,直到我的客户端尝试调用访问我创建的库(Gamez)的服务。它调用的方法正常工作,只是通过WCF它突然崩溃了 因此,它呼吁:C# 尝试访问库时WCF引发异常,c#,wcf,visual-studio-2012,C#,Wcf,Visual Studio 2012,我是一名试图建立WCF服务的学生。我让主机和客户端使用servicelibrary运行,它工作正常,直到我的客户端尝试调用访问我创建的库(Gamez)的服务。它调用的方法正常工作,只是通过WCF它突然崩溃了 因此,它呼吁: public int AddValues(int a, int b) { int c = a + b; return c; } 这很好,但是: public Guid GetUserId(String userName) {
public int AddValues(int a, int b)
{
int c = a + b;
return c;
}
这很好,但是:
public Guid GetUserId(String userName)
{
Guid user = Gamez.Sql.GetIdFromUserName(userName);
return user;
}
=吊杆。请记住,当从非WCF相关代码调用GetIdFromUserName方法时,该方法工作正常
这是客户端代码:
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
client.Open();
String gameName = "Liksomspill";
String userName = "Grouse";
//int result = client.GetHighScore(userName, gameName);
//Console.WriteLine("highscoren er: {0}", result);
//Guid result = client.GetUserId(userName);
//Console.WriteLine("blablabla: {0}", result);
int a = 5;
int b = 2;
int result2 = client.AddValues(a, b);
Console.WriteLine("highscoren er {0}", result2);
Console.WriteLine();
Console.ReadLine();
client.Close();
}
}
}
只要保持这种状态(因此它将返回7),就可以正常工作,但如果我删除这些行中的注释:
Guid result = client.GetUserId(userName);
Console.WriteLine("blablabla: {0}", result);
顶行将抛出一个异常,但我不知道它是什么,因为它只说“FaultException`1未处理”,子文本是“Gamez.Sql'的类型初始值设定项抛出了一个异常”。我试着查找这个问题,但我真的不知道如何解决这个问题(我正在VisualStudio2012中编写代码)。以下是我在这个主题上找到的一些链接,但它让我非常困惑:
我确信有一些基本的东西是我完全监督的(…你应该能够通过wcf服务调用库,对吗?或者我必须将我的库中我想要使用的部分合并到服务库中?)。我已经添加了对库的引用,所以这不应该是个问题。如果这个问题有点愚蠢的话,我很抱歉,但是在我的头撞了这个问题六个小时之后,我在沮丧中撕裂了自己
如果你需要一些背景信息,我基本上是在为上传到我制作的网站上的游戏设置API(这样游戏可以从数据库中提取/插入高分等)
非常感谢您抽出时间
如果需要,以下是主机代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using WcfServiceLibrary;
using System.ServiceModel.Description;
namespace WCFHost
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/WCF");
ServiceHost selfHost = new ServiceHost(typeof(Service1), baseAddress);
ServiceDebugBehavior debug = selfHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (debug == null)
{
selfHost.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
try
{
//selfHost.Open();
selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "Service1Service");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready");
Console.WriteLine("Press enter to terminate service");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
执行此操作:在调试器(VS2012)下启动服务。进入“调试>异常”菜单。在对话框中,选中
抛出列下的所有复选框。然后运行您的客户机。它将带您到异常发生的地方,并告诉您它到底是什么。@YK1您好,谢谢您的回复。我照你说的做了,再次运行了主机和客户端。我也犯了同样的错误。但是,我必须在调试模式之外运行主机,因为我不能同时运行这两个模式(在调试模式下,所以主机按ctrl+f5,然后在调试模式下运行客户机)。也许这就是为什么?你必须在调试器下运行主机。不需要在调试器下运行客户端。只需在命令行上运行它。@YK1非常感谢,这使我更接近于一个解决方案。我所说的库是一个包含大量调用的SQL库,当它试图设置连接字符串public static string connectionString=System.Configuration.ConfigurationManager.ConnectionStrings[“webtek.uia.no”]。connectionString代码>在库本身中。它给了我一个错误“对象引用未设置为对象的实例”。但我不明白为什么,因为当我从网页代码中调用它时,这一行运行正确。请检查网页的web.config中的连接字符串。这些必须复制到wcf主机的app.config。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Gamez;
namespace WcfServiceLibrary
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public void SetHighScore(int score, String userName)
{
}
public int GetHighScore(String userName, String gameName)
{
int score = Gamez.Sql.getHighScore(userName, gameName);
return score;
}
public Guid GetUserId(String userName)
{
Guid user = Gamez.Sql.GetIdFromUserName(userName);
return user;
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public int AddValues(int a, int b)
{
int c = a + b;
return c;
}
}
}