我试图验证传递的变量是否可以转换为特定类型.我已经尝试了以下但是无法让它编译,所以我假设我正在以错误的方式(我是C#的新手)
string myType = "System.Int32";
string myValue = "42";
bool canBeCast = false;
try
{
// try to convert the value to it's intended type to see if it's valid.
var result = (Type.GetType(typeString))dataValue;
canBeCast = true;
}
catch
{
canBeCast = false;
}
Run Code Online (Sandbox Code Playgroud)
我基本上试图避免一个大规模的开关声明
switch(myType){
case "System.Int32":
try
{
var convertedValue = Convert.ToInt32(myValue);
}
catch (Exception)
{
canBeConverted = false;
}
break;
case "another type":
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:
好的,基本上我有一个已知输入类型的db表,如下所示:
CREATE TABLE [dbo].[MetadataTypes] (
[typeName] VARCHAR (50) NOT NULL,
[dataType] …Run Code Online (Sandbox Code Playgroud) 我是EF5 Code First的新手,在开始工作项目之前,我正在修改概念验证.
我最初创建了一个看起来像的模型
public class Person {
public int Id { get; set; }
public string FirstName { get; set;}
public string Surname {get;set;}
public string Location {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我添加了一些记录,使用了一个我坚持在顶部的MVC应用程序.
现在我想将Location列更改为枚举,如:
public class Person {
public int Id { get; set; }
public string FirstName { get; set;}
public string Surname {get;set;}
public Locations Location {get;set;}
}
public enum Locations {
London = 1,
Edinburgh = 2,
Cardiff = 3
}
Run Code Online (Sandbox Code Playgroud)
当我添加新的迁移时,我得到:
AlterColumn("dbo.People", "Location", c => c.Int(nullable: false));
Run Code Online (Sandbox Code Playgroud)
但是当我运行update-database时出现错误 …
我创建了一个ASP.NET Core MVC/WebApi站点,该站点有一个RabbitMQ订阅者,基于James Still的博客文章Real-World PubSub Messaging with RabbitMQ.
在他的文章中,他使用静态类来启动队列订阅者并为排队事件定义事件处理程序.然后,此静态方法通过静态工厂类实例化事件处理程序类.
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace NST.Web.MessageProcessing
{
public static class MessageListener
{
private static IConnection _connection;
private static IModel _channel;
public static void Start(string hostName, string userName, string password, int port)
{
var factory = new ConnectionFactory
{
HostName = hostName,
Port = port,
UserName = userName,
Password = password,
VirtualHost = "/",
AutomaticRecoveryEnabled = true,
NetworkRecoveryInterval = TimeSpan.FromSeconds(15)
};
_connection = factory.CreateConnection();
_channel = …Run Code Online (Sandbox Code Playgroud) c# dependency-injection rabbitmq service-locator asp.net-core
我有一个最初使用beta位的web api服务,我已经使用候选版本位进行了重建,现在我遇到了这个问题.
我有一个POST操作,它将复杂选项作为唯一参数.当我以json格式发送请求时,对象按预期反序列化,但如果我发送XML而不是参数为null.
在测试版中,我通过禁用模型绑定来解决这个问题,如Carlos Figueira的博客文章" 禁用ASP.NET Web API Beta上的模型绑定"中所述
然而,在RC中,他们已经删除了此方法正在实现的IRequestContentReadPolicy.
我的行动:
public List<Models.Payload> Post([FromBody]Models.AimiRequest requestValues)
{
try
{
if (requestValues == null)
{
var errorResponse = new HttpResponseMessage();
errorResponse.StatusCode = HttpStatusCode.NotFound;
errorResponse.Content = new StringContent("parameter 'request' is null");
throw new HttpResponseException(errorResponse);
}
var metadataParams = new List<KeyValuePair<string, string>>();
foreach (Models.MetadataQueryParameter param in requestValues.Metadata)
{
metadataParams.Add(new KeyValuePair<string, string>(param.Name, param.Value));
}
List<Core.Data.Payload> data = _payloadService.FindPayloads(metadataParams, requestValues.ContentType, requestValues.RuleTypes);
var retVal = AutoMapper.Mapper.Map<List<Core.Data.Payload>, List<Models.Payload>>(data);
return retVal; // new HttpResponseMessage<List<Models.Payload>>(retVal);
}
catch (System.Exception ex) …Run Code Online (Sandbox Code Playgroud) 我在从.NET 2.0升级到.NET 4.0的webforms应用程序中有一个System.Web.UI.WebControls.Xml控件(Xml1)
我从代码隐藏页面得到两个警告,我想做些什么.
...
Dim ds As DataSet = app.GetObjects
Dim xmlDoc As New System.Xml.XmlDataDocument(ds)
Xml1.Document = xmlDoc
Xml1.TransformSource = "~/xslt/admin_objectslist.xslt"
...
Run Code Online (Sandbox Code Playgroud)
从第二行我得到警告:
'System.Xml.XmlDataDocument'已过时:'XmlDataDocument类将在以后的版本中删除.'.
从第三行我得到警告:
'Public Property Document As System.Xml.XmlDocument'已过时:'建议的替代方法是XPathNavigator属性.创建System.Xml.XPath.XPathDocument并调用CreateNavigator()以创建XPathNavigator.
推荐的.NET 4.0替代品是什么?
我有一个ASP.NET MVC 2应用程序,我在其中创建自定义操作筛选器.此过滤器位于应用程序中的控制器上,并从数据库验证该功能当前是否可用.
Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
Try
' Check controller name against database.
Dim controllerName = filterContext.Controller.GetType().Name
controllerName = controllerName.Remove(controllerName.Length - 10)
' Look up availability.
Dim available As Boolean = _coreService.GetControllerAvailability(controllerName)
If Not available Then
' Redirect to unavailable notice.
filterContext.Result = New RedirectResult("/Home/Unavailable/")
End If
Catch ex As Exception
_eventLogger.LogWarning(ex, EventLogEntryType.Error)
Throw
End Try
End Sub
Run Code Online (Sandbox Code Playgroud)
我的问题是,根据已请求的操作,我需要将用户重定向到返回视图,部分视图或JSON的操作.
鉴于ActionExecutingContext,我可以找出最初请求的操作的返回类型是什么?
编辑:
好吧,我越来越近但又有另一个问题.
Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
Try
' Check controller name against database. …Run Code Online (Sandbox Code Playgroud) 我已将VS2008 ASP.NET MVC解决方案迁移到VS2010/MVC2/.NET 4.0.该解决方案在本地构建,所有单元测试都通过.
我们的TFS服务器仍然是TFS2008,我在通过CI构建时遇到了问题.
所有项目都成功构建,单元测试所有运行并通过但运行测试项目失败.
我关注了如何使构建工作的博客文章,我几乎就在那里.
梳理日志文件失败后我发现了以下内容:
Test Run Completed.
Passed 1101
------------
Total 1101
Results file: C:\Documents and Settings\apptemetrybuild\Local Settings\Temp\Client Portal 3\CI\TestResults\apptemetrybuild_ATT15DEV01 2010-04-27 09_09_59_Any CPU_Release.trx
Test Settings: Default Test Settings
Waiting to publish...
Publishing results of test run apptemetrybuild@ATT15DEV01 2010-04-27 09:09:59_Any CPU_Release to http://att15tfs01:8080/...
.....Publish completed successfully.
Command:
D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe /nologo /searchpathroot:"C:\Documents and Settings\apptemetrybuild\Local Settings\Temp\Client Portal 3\CI\Binaries\Release" /resultsfileroot:"C:\Documents and Settings\apptemetrybuild\Local Settings\Temp\Client Portal 3\CI\TestResults" /testcontainer:"C:\Documents and Settings\apptemetrybuild\Local Settings\Temp\Client Portal 3\CI\Binaries\Release\\Attenda.Stargate.Security.Tests.dll" /publish:"http://att15tfs01:8080/" /publishbuild:"vstfs:///Build/Build/149" /teamproject:"Client Portal …Run Code Online (Sandbox Code Playgroud) 我已将我的sql server 2005数据库导入到VS2010数据库项目中.我的一个存储过程包含类似于的语句
插入#myTemp ...
和Visual Studio给我一个警告,如
SQL04151:过程:[dbo].[mySproc]有一个未解析的对象[#myTemp]的引用.
有没有办法解决这个问题?我想尽可能多地清除项目警告.
我有一个简单的Web服务,允许应用程序查询我的CMDB.我遇到问题的函数使用一个小的结果集但是失败了一个较大的结果集,表明它是WCF服务配置中阻止它成功的东西.
我有一个简单的WinForms测试应用程序,其中包含对Web服务的服务引用和一个调用相关函数的函数.
较小的结果集返回~120KB的xml,失败的较大结果集约为2MB.我已经尝试增加maxReceivedMessageSize和maxStringContentLength的大小而没有成功.
我错过了一些配置吗?如果那是问题,我会期待更详细的错误消息.
提前致谢,
缺口
返回的错误是:
System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. --->
System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, …Run Code Online (Sandbox Code Playgroud) 我在使用SignalR实际使用网络套接字时遇到问题.它是使用ASP.NET MVC构建的Intranet应用程序.
为了测试,我使用SignalR v2.1.2 将ASP.NET站点中的简单聊天应用程序放在一起.
我把它放在我们的一个开发服务器(Windows Server 2012,IIS8,.NET 4.5.1)上,我在Windows 7上使用Chrome.
查看Chrome开发工具的"网络"标签,它与ServerSentEvents连接.谈判呼叫看起来像这样
ConnectionId: "eee14afe-6598-492e-81ae-f14b98041474"
ConnectionTimeout: 110
ConnectionToken: "1iDREVWa++NL0Cj95i++QOufi+1+ywZlY1Tsrv7t06zqca5wn6nvPRLJQOd8/I7EPEaDGabP1MfqhoR/Q2D1vFK6bdujXdFoxGYF XlZ00AHLw9qjDuDgyx3+6a7qXHGFKp6ag6zu4TGuuW9sqM/nkw=="
DisconnectTimeout: 30
KeepAliveTimeout: 20
LongPollDelay: 0
ProtocolVersion: "1.4"
TransportConnectTimeout: 5
TryWebSockets: false
Url: "/Chat/signalr"
Run Code Online (Sandbox Code Playgroud)
然后我将同一个应用程序复制到生产服务器,再次是Windows 2012 Server,IIS8,.NET 4.5.1.
这次应用程序使用Web套接字进行连接,协商调用如下所示:
ConnectionId: "27484ca2-2935-43fd-a6eb-ae13f7fdbd54"
ConnectionTimeout: 110
ConnectionToken: "AhL+5jrvoHa0FXdU+Zg3dJ4a3Avfw28aOZN7raTHpYxUte4GA5Ru9ZmdDZMtCZOFbnpGzOmktY56jH2Tbz+I7g3OO5RnMlBBn8CSg5Il8fb0p/faK1ShGbWhFOKBe9Ns"
DisconnectTimeout: 30
KeepAliveTimeout: 20
LongPollDelay: 0
ProtocolVersion: "1.4"
TransportConnectTimeout: 5
TryWebSockets: true
Url: "/Chat/signalr"
Run Code Online (Sandbox Code Playgroud)
由于未知原因,这次TryWebSockets为True.
任何人都可以告诉我为什么在第一个例子中它不试图使用Web套接字?可能是IIS配置?我确保两个实例在其应用池中具有相同的设置.
可能是防火墙问题?这两台服务器位于网络的不同部分,但我不确定它们之间的区别.也许这是路由器问题?
这些应用已在Chrome v38和IE 11中进行了测试.
欢迎任何想法/解决问题的步骤.
编辑:
好的,我在ASP.NET网站上的本文后面的服务器和javascript中启用了跟踪
服务器生成带有以下内容的传输日志文件:
SignalR.Transports.TransportHeartBeat Information: 0 : Connection 6b0193e6-bde6-4130-b4da-178fd24a786a is New. …Run Code Online (Sandbox Code Playgroud)