c# - 在Azure上部署WCF服务,尝试访问数据库时超时

标签 c# .net wcf unit-testing azure

我有第一个项目“MyProject.Core”,我有 EDMX:

repo 类:

class MyProjectRepo : IMyProjects
{
    public int NumberOfUser()
    {
        return new context().User.Count();
    }

    public string HelloWorld()
    {
        return "Hello World!";
    }
}

界面:

public interface IMyProjects
{
    int NumberOfUser();
    string HelloWorld();
}

工厂:

public static class MyProjectFactory
{
    private static IMyProjects _returnedObject;

    public static IMyProjects GetObject()
    {
        lock (typeof(MyProjectFactory))
        {
            _returnedObject = new MyProjectRepo();
        }
        return _returnedObject;
    }
}

测试项目“MyProject.Core.Tests”(测试通过):

[Test]
public void NumberOfUser_Test()
{
    var number = MyProjectFactory.GetObject().NumberOfUser();
    Assert.AreEqual(1, number);
}

[Test]
public void HelloWorld_Test()
{
    var hello = MyProjectFactory.GetObject().HelloWorld();
    Assert.AreEqual("Hello World!", hello);
}

我创建了一个“云”项目和一个 WCFServiceWebRole。

在 WCFServiceWebRole 中,我有这个:

public class Service1 : IService1
{
    public int NumberOfUser()
    {
        return MyProjectFactory.GetObject().NumberOfUser();
    }

    public string Hello()
    {
        return MyProjectFactory.GetObject().HelloWorld(); 
    }
}

[ServiceContract]
public interface IService1
{
    [OperationContract]
    int NumberOfUser(string login, string password);

    [OperationContract]
    string Hello();
}

一个测试WCF的项目,方法“Hello”返回正确的值。 这是我遇到问题的另一种方法。在 app.config 中,我有这个 :

<bindings>
    <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" />
    </basicHttpBinding>
</bindings>

<client>
    <endpoint address="http://myproject.azurewebsites.net/Service1.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
        contract="myprojectServiceAzure.IService1" name="BasicHttpBinding_IService1" />
</client>

错误:

System.TimeoutException : The request channel timed out while waiting for a reply after 00:00:59.7138476. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. ----> System.TimeoutException : The HTTP request to 'http://MyProject.azurewebsites.net/Service1.svc' has exceeded the allotted timeout of 00:00:59.9270000. The time allotted to this operation may have been a portion of a longer timeout. ----> System.Net.WebException : The operation has timed out MyProject.Azure.Tests\Service References\MyProjectServiceAzure\Reference.cs(487, 0) : MyProject.Azure.Tests.PointageServiceAzure.Service1Client.MyMethod(String login, String password) MyProject.Azure.Tests\AzureTests.cs(18, 0) : MyProject.Azure.Tests.AzureTests.Test()

最佳答案

您检查过 Azure 防火墙规则吗?您需要“允许”WCF 服务访问 SQL Azure 数据库。根据内存,您需要选中一个复选框,该复选框允许托管应用程序访问数据库,因为默认情况下未选中此复选框。

关于c# - 在Azure上部署WCF服务,尝试访问数据库时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14068663/

相关文章:

c# - 使用用户名和密码连接到网络驱动器

c# - HTTP 415 无法处理消息,因为内容类型 'application/json; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'

c# - 具有单个 web.config 的 WCF HTTPS 和 HTTP 绑定(bind)

c# - 在负载均衡器和 SSL 后面配置 WCF

.net - 如何获取 'codebehind' 文件,以便默认创建 RC1 中的 ASP.NET-MVC View

c# - 类库、Silverlight 和 Web 服务

c# - 枚举值未在生成的 wsdl 文件中正确显示。为什么?

c# - MSMQ,消息被放入队列并消失但从未被服务契约(Contract)接收

c# - 是否有用于注释 C# 代码的标准(如 phpdoc 或 python 的文档字符串)?

c# - 自动按下 iframe 中的按钮