我正在尝试插入XML列(SQL SERVER 2008 R2),但服务器抱怨:
System.Data.SqlClient.SqlException(0x80131904):
XML解析:第1行,第39个字符,无法切换编码
我发现XML列必须是UTF-16才能使插入成功.
我正在使用的代码是:
XmlSerializer serializer = new XmlSerializer(typeof(MyMessage));
StringWriter str = new StringWriter();
serializer.Serialize(str, message);
string messageToLog = str.ToString();
Run Code Online (Sandbox Code Playgroud)
如何将对象序列化为UTF-8字符串?
编辑:好的,抱歉混淆 - 字符串需要是UTF-8.你是对的 - 默认情况下它是UTF-16,如果我尝试以UTF-8插入它就会通过.所以问题是如何序列化为UTF-8.
例
这会在尝试插入SQL Server时导致错误:
<?xml version="1.0" encoding="utf-16"?>
<MyMessage>Teno</MyMessage>
Run Code Online (Sandbox Code Playgroud)
这不是:
<?xml version="1.0" encoding="utf-8"?>
<MyMessage>Teno</MyMessage>
Run Code Online (Sandbox Code Playgroud)
更新
我想出当SQL Server 2008的Xml列类型需要utf-8时,以及当encoding你尝试插入xml规范的属性中的utf-16时:
如果要添加utf-8,请将参数添加到SQL命令,如下所示:
sqlcmd.Parameters.Add("ParamName", SqlDbType.VarChar).Value = xmlValueToAdd;
Run Code Online (Sandbox Code Playgroud)
如果您尝试encoding=utf-16在上一行中添加xmlValueToAdd,则会在插入中产生错误.此外,VarChar意味着国家字符不被识别(它们变成问号).
要将utf-16添加到db,请使用SqlDbType.NVarChar或SqlDbType.Xml在前面的示例中,或者根本不指定type:
sqlcmd.Parameters.Add(new SqlParameter("ParamName", xmlValueToAdd));
Run Code Online (Sandbox Code Playgroud) 我需要一个单例类来实例化一些参数.我现在这样做的方式是:
class SingletonExample
{
private SingletonExample mInstance;
//other members...
private SingletonExample()
{
}
public SingletonExample Instance
{
get
{
if (mInstance == null)
{
throw new Exception("Object not created");
}
return mInstance;
}
}
public void Create(string arg1, string arg2)
{
mInstance = new SingletonExample();
mInstance.Arg1 = arg1;
mInstance.ObjectCaller = new ObjectCaller(arg2);
//etc... basically, create object...
}
}
Run Code Online (Sandbox Code Playgroud)
该实例是"迟到"创建的,这意味着我没有在app启动时获得所有必需的参数.
一般来说,我不喜欢强制方法调用的排序,但我在这里看不到另一种方式.IoC也不会解决它,因为我可以在容器中注册它,我也可以调用Create()...
你认为这是一个好的方案吗?你有其他想法吗?
编辑:我知道我写的一个例子,它不是线程安全的,线程安全不是问题的一部分
我一直在尝试构建一个带有提示的文本框,当它显示为空时显示.我在一个样式中设置提示文本时遇到问题.
确切地说,这是有效的(也就是说,它正确绑定):
<TextBox Tag="hint text">
<TextBox.Background>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" FontStyle="Italic" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Background>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
但是,当我将它移动到Style时,它不会:
<Style TargetType="TextBox" x:Key="stlHintbox">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
<Setter Property="Background">
<Setter.Value>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Tag="inner" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}"
FontStyle="Italic" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBox Tag="hint text" Style="{StaticResource stlHintbox}" />
Run Code Online (Sandbox Code Playgroud)
那捕获的是什么?如何从样式中绑定祖先属性?
可能重复:
Nullable类型不是可空类型?
在以下代码中:
DateTime? dt = DateTime.Now;
MessageBox.Show(dt.GetType().ToString());
Run Code Online (Sandbox Code Playgroud)
消息框显示"System.DateTime",而不是Nullable<DateTime>.以下内容也返回false(因为GetType错误):
if (dt.GetType().IsAssignableFrom(typeof(DateTime?)))
...
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,使用DateTime?或Nullable<DateTime>没有区别)在监视窗口中,您有"类型"列显示正确的类型(System.DateTime?).
在我的代码中,我引用了dt作为一个object,所以我需要正确地获取底层类型.我怎样才能做到这一点?
我们的服务使用从小到大的数据集(文档生成),并且它适用于某些调用,但对于某些特定请求(完全相同的方法,不同的参数),它只返回:
System.ServiceModel.EndpointNotFoundException:在http:// localhost:8010/MyService/MyService.svc上没有可以接受该消息的端点.这通常是由错误的地址或SOAP操作引起的.有关更多详细信息,请参阅InnerException(如果存在).---> System.Net.WebException:远程服务器返回错误:(404)Not Found.
请注意,该服务正在运行,文档已生成,但正如我所说的并非全部......(并且可以从浏览器打开服务)
我已经在web.config中打开了跟踪(system.diagnostics),并且没有在svclog中获得更多信息.
绑定(wsHttp)配置为:
<binding name="wsHttpWithTrans" transactionFlow="True" messageEncoding="Mtom" maxReceivedMessageSize="65536000" maxBufferPoolSize="124288000">
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="16384000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
Run Code Online (Sandbox Code Playgroud)
而且,还有:
<configuration>
<system.web>
<httpRuntime maxRequestLength="124288000" />
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我相信这个消息应该在maxReceivedMessageSize其他属性的范围内.
目前我对消息的大小持怀疑态度,但不能确定 - 您是否知道如何进一步调试?
我正在编写一个XSLT转换(对于XSL-FO),并且需要为字符串值中的每个字母重复一些内容,例如:
如果字符串存储在MyData/MyValue字符串中(例如MyData.MyValue ="something"),我需要像这样的for-each:
<xsl:for-each select="MyData/MyValue"> <!-- What goes here to iterate through letters? -->
<someTags>
<xsl:value-of select="Letter" /> <!-- What goes here to output current letter? -->
</someTags>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我创建了一个OWIN托管WebAPI 2.还有一个Web应用程序(AngularJS),它使用API并充当客户端.
我已经添加了必要的代码CORS到Startup.cs,并主持它本地IIS比客户的不同端口上,并确认其修复Cors问题.
然后,我将这两个应用程序部署到Azure(我已将两个应用程序都放在Azure上作为Web应用程序,我也尝试将OWIN放到当前处于预览状态的Azure API中)但是 - 预检请求现在失败了(没有Access-Control-Allow-Origin出现在响应).
问:我不知道Azure的某些特定内容吗?为什么OWIN在部署时没有提供此标头,但是它正在使用localhost?我没有在应用程序的Azure刀片设置的属性窗口中看到任何约束.
笔记:
关于我正在使用的设置的一些细节:
Owin,WebAPI2,Ninject,SignalR*Startup.cs的相关部分:
public void Configuration(IAppBuilder appBuilder)
{
appBuilder.UseCors(CorsOptions.AllowAll);
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//bind IClientsNotifier with method returning singleton instance of hub
var ninjectKernel = NinjectWebCommon.GetKernel();
ninjectKernel.Bind<MySignalRHub>().ToSelf().InSingletonScope();
ninjectKernel.Bind<QueryStringBearerAuthorizeAttribute>().ToSelf();
GlobalHost.DependencyResolver = new NinjectSignalRDependencyResolver(ninjectKernel);
appBuilder.Map(
"/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration();
map.RunSignalR(hubConfiguration); …Run Code Online (Sandbox Code Playgroud) 我有一个带有自定义控件的表单.
我的表格中有一个方法:
private void SetEnabledOnControls(bool val)
{
if (InvokeRequired)
{
Invoke((Action<bool>)SetEnabledOnControls, val);
}
else
{
//do the work - iterate over child controls,
//and they iterate over their children, etc...
}
}
Run Code Online (Sandbox Code Playgroud)
在else分支上的方法内部,我得到了提到的异常:
Cross-thread operation not valid: Control 'txtNumber' accessed from a thread other than the thread it was created on.
我的场景实际上有点复杂 - 我只是将其推断为一个例子.实际上发生的是我正在使用WorkflowFoundation - 我在WorkflowApplication中运行StateMachineActivity(CTP1)(它在自己的线程中运行),我订阅了它的事件,并从那里调用SetEnabledOnControls.此外,我正在使用书签来恢复我的工作流程(而且,旁边有MEF,没有涉及场景).
所有这些都与我对InvokeRequired的明显误解无关 - 如果InvokeRequired为false,我有可能有交叉线程异常吗?我没有"手动"创建任何控件 - 它都在设计者放置的Initialize()中.
任何人都可以对此有所了解吗?
谢谢!
编辑
使用GWLlosa建议,我使用了跟踪ThreadId System.Threading.Thread.CurrentThread.ManagedThreadId.现在来了奇怪的部分...... Initialize()中的线程id是10.在传递前2个状态之间,它带有Id 13 - InvokeRequired为true,并且它被正确调用.但是,在第二个状态之后,当它SetEnabledOnControls再次进入13时,但这次InvokeRequired是假的!怎么会!?当然,后来它无法改变儿童控制(这并不奇怪).可能是表格以某种方式改变了它所生活的线程?
编辑2 现在我打电话给:
if …Run Code Online (Sandbox Code Playgroud) 我有存储过程(它们接受参数)返回多个结果集,我想将它保存在一个类型化的数据集中.
问题是:我可以让Visual Studio 2010根据存储过程生成类型化数据集吗?我知道我可以将它用于单个表,但我需要整个dataSet(多个表,每个结果集一个)
stored-procedures dataset strongly-typed-dataset visual-studio-2010
假设我们有大量配置的集成测试IConfiguration。我已经将测试设置为使用 autofac 容器,现在我想使用 Mock 来替换对其中一个属性的操作,而无需模拟或替换其他所有内容:
var config = MyTestContainer.Resolve<IConfiguration>();
//let's say that config.UseFeatureX = false;
//here, I'd like to create mock "around" the existing instance:
var mockedConfig = Mock.CreateWith(config); //CreateWith => a method I'd like to find how to do
mockedConfig.Setup(c => c.UseFeatureX).Returns(true);
Run Code Online (Sandbox Code Playgroud)
如何围绕现有实例进行这种包装?它应该类似于.CallBase但不仅仅是调用基本实现,我希望有一种方法可以调用基本值。