尝试访问库时WCF抛出异常

时间:2013-05-24 23:14:48

标签: 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)
    {
        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'的类型初始化程序引发异常。”我试过这个,但我真的不明白如何解决这个问题(我在Visual Studio 2012中进行了编码)。以下是我在这个主题上找到的一些链接,但这只是让我感到困惑:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/1829b844-1237-4390-8d36-76a7e42a64d3/

http://sergecalderara.wordpress.com/2008/11/25/systemservicemodelfaultexception1-was-unhandled-by-user-code/

我确信只有一些基本的东西我完全在监督(...你应该能够使用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();
            }
        }
    }
}

这是ServiceLibrary代码:

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;

        }
    }
}

0 个答案:

没有答案