nLog and TransactionScope - asp.net-mvc-3

I have an ASP.NET MVC 3 (using Entity Framework 4.2) application that uses transactions as follows:
using (var transaction = new TransactionScope())
{
// Database action 1
// Database action 2
context.SaveChanges();
Logger.Info("Record X updated");
transaction.Complete();
}
I get no errors, but no data is written to the database. However, the log file target (there just for testing purposes) works fine. Here is my nLog (I'm using v2) config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogToConsole="true"
internalLogToConsoleError="true" >
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}app_data\file.txt" />
<target xsi:type="Database" name="database">
<commandText>INSERT INTO [LogEntries] (TimeStamp, Message, Level, Logger) VALUES(GETDATE(), #msg, #level, #logger)</commandText>
<parameter name="#msg" layout="${message}" />
<parameter name="#level" layout="${level}" />
<parameter name="#logger" layout="${logger}" />
<dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
<connectionString>Data Source=${basedir}app_data\Logger.sdf</connectionString>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="database" />
</rules>
</nlog>
If I move the Logger line outside of the transactionscope it works fine. So I think it's something to do with this. I'm using SQL Server 2008 r2 for my main database and SQL Compact 4 for my logging database. Also, I've tried adding useTranscations="true".
Any ideas what I'm doing wrong?
Thanks
Alan

You are trying to have the transaction span two different servers, which is a distributed transaction. Unfortunately, SQLCE does not support distributed transactions (from the documentation):
Distributed transactions are not supported in SQL Server Compact. Therefore, a local transaction will not be automatically promoted to a fully distributable transaction.
I am curious why you don't log to the SQL 2008 R2 database, but if you must log to SQLCE, I think you'll have to track whether the transaction is committed and then determine whether to call NLog after the the TransactionScope block completes.

Related

ODP.NET + Unexpected traffic on port 6200 (Oracle Notification Services)

We've got a client using our product, which includes a windows service built using .NET 4+, Entity Framework, and ODP.NET. For some reason in one particular installation, but not any other, the windows service appears to be generating traffic on 6200. Oracle Documentation indicates this is the port Oracle Notification Services uses. This is unexpected because our product does not utilize ONS. I've found other Oracle Documentation that indicates that RAC / Fast Failover also uses ONS, but we've confirmed with the client that they do not use RAC in any environments, and we are not using the RAC options in the connection string. We are also not (to our knowledge) using Database Change Notifications or doing any cache/verification of cached data, which it sounds like also leverages ONS.
Can anyone help explain why we would be generating port 6200 traffic, and more importantly, how to get it to stop? Thanks!
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings [Redacted] />
<appSettings [Redacted] />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</providers>
<contexts>
<context type="[Redacted]" disableDatabaseInitialization="true" />
</contexts>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client" />
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" />
</runtime>
</configuration>
We've narrowed the culprit down to one or more of 3 Oracle / ODP.NET specific Connection String settings:
ENLIST, HA EVENTS, and/or LOAD BALANCING
Oracle's documentation on Fast Application Notification tipped us off to these settings which we had not been aware of. This lead us to Oracle's documentation on all of their Feature Connection String Attributes.
Enlist - Default "true" - Serviced components automatically enlist in
distributed transactions.
HA Events - Default "false" - Enables ODP.NET connection pool to
proactively remove connections from the pool when a RAC service,
service member, or node goes down.
Load Balancing - Default "false" - Enables ODP.NET connection pool to
balance work requests across RAC instances based on the load balancing
advisory and service goal.
We set all 3 to explicitly be false, and the port 6200 connections ended.
ENLIST=false; HA EVENTS=false; LOAD BALANCING=false;
If the defaults are to be believed then Enlist was the culprit, but client's availability did not allow for discrete testing to confirm. We've come across other various documentation linking all 3 of these pieces of functionality to Oracle Notification Services & port 6200.
It may depend on the installed Oracle client version. In 12.1.0.2 auto-configuration of ONS feature was released. It may have something like ONS auto-discovery at the beginning of establishing a DB connection which produces this traffic.

NLog not writing in Asp.Net Web Api App

I have been trying to implement NLog as a trace writer in an Asp.Net Web Api 2.2 app. This is my web.config:
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" throwExceptions="true" internalLogFile="c:\file.txt">
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/log.txt"/>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile"/>
</rules>
</targets>
During debugging this works fine if I am using IIS Express as the host, but If I set the project to use Local IIS it fails with a System.UnauthorizedAccessException. I am running VS 2013 as an administrator too.
You should check whether the user, which is configured in the Local IIS to be used to run the appdomain of your web application, have the sufficient access rights to write to the ${basedir}/Logs directory.
Please, ensure that you can run this code from your application:
File.WriteAllText(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs\test.txt"),
"Some content");
When you are able to do this, you'll also would be able to do the logging

ADO.NET EF - can't connect to my DB (using Visual Studio 2010)

So I'm struggling with this problem for more than 4 hours and I don't know where to look at any more. I follow this tutorial for absolute beginners in ADO.NET Entity Framework - [See The tutorial][1] [1]: http://msdn.microsoft.com/en-us/data/jj193542
I'm using Microsoft SQL Server 2012 and I'm using this App.Config file for my project :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=sqlserver2012; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
But I keep getting errors and when I use this code the error is : **An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string.**
I'm totally new to ADO.NET Entity Framework but as I see it the problem maybe is in defaultConnectionFactory configuration. But anyways I can't find out how to keep on and how to solve this problem.
if you in fact have SQL Server instance named sqlserver2012 on your localhost zour connectionString should look like this
<parameter value="Data Source=localhost\sqlserver2012; Integrated Security=True; MultipleActiveResultSets=True" />
Cheers,
T.

ADO.NET EF - can't establish DB connection using VS2010

I'm following this tutorial : http://msdn.microsoft.com/en-us/data/jj193542 for Code First basics using ADO.NET EF. The problem is that when executing the code the DataBase is not created automatically and I think that this is because the configurations in my app.config but it's my first day with real code and I can't figure out how to adjust my config file so I can connect to my server and use a created DataBase if neccessary or let the program from the tutorial create new database as expected from what is written.
This is my App.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<!-- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory> -->
<connectionStrings>
<add name="BloggingContext"
providerName="System.Data.SqlClient"
connectionString="server=milka-pc\sqlserver2012;Database=Panorama;user id=MyID;password=MyPass;port=3333;Integrated Security=True;"/>
</connectionStrings>
<!--<contexts>
<context type=" Blogging.BloggingContext, MyAssembly">
<databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
</context>
</contexts>-->
</entityFramework>
</configuration>
As you can see some parts are commented as I tried a various things to make it work.
This is how y server explorer looks like while I'm trying to connect:
Also since I have zero experience with ViasualStudioXXXX and connecting to DataBases this is what I see and how I select my server:
There in milka-pc\sqlserver2012 I have a DB named Panorama which I can use or better follow the tutorial step by step and leave the program to create my new DB. For now I can't neither of these two. And to connect to milka-pc\sqlserver2012 I need to provied UserName and Password which I did in App.config. I say this just to know that these fields are not blank
If you are sure that milka-pc\sqlserver2012 is in fact working on port 3333 as you suggested
change your connectionstring to this:
<add name="BloggingContext"
providerName="System.Data.SqlClient"
connectionString="server=milka-pc\sqlserver2012,3333;Database=Panorama;user id=MyID;password=MyPass;Integrated Security=True;"/>
Just to make sure try that connection string in you SQL management studio with provided credentials.
If that doesnt work consult your sql server configuration manager, specifically under SQL server Network configuration make sure that your instance has enabled TCP/IP pipe with appropriate port enabled.
Cheers.

How can i validate SPML SOAP Requests in Spring Web Services if their XSDs have Unique Particle Attribution Errors?

I was wondering if anyone could point me in the right direction here.
I'm working on a project that needs to create web service functionality to adhere to the SPML v2 Spec (https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=provision). It's an XML based spec for provisioning services.
I've created the end points that map to the namespace and localpart of the soap requests fine. My issue lies in attempting to validate the payload requests in spring web services using the provided XSDs from the spec itself. From my research, it is an "Open Content Model", and any validation or attempt to compile the XSDs results in Unique Particle Attribution errors.
This happens because the CORE XSD has an "ExtensibleType" complexType that when other complexTypes extend from it causes the error. From my research, i can see why the error is occurring, but i don't want to modify the xsds provided by the spec itself.
Example:
<complexType name="ExtensibleType">
<sequence>
<any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</sequence>
<anyAttribute namespace="##other" processContents="lax"/>
</complexType>
<complexType name="SearchQueryType">
<complexContent>
<extension base="spml:ExtensibleType">
<sequence>
<annotation>
<documentation>Open content is one or more instances of QueryClauseType (including SelectionType) or LogicalOperator.</documentation>
</annotation>
<element name="basePsoID" type="spml:PSOIdentifierType" minOccurs="0" />
</sequence>
<attribute name="targetID" type="string" use="optional"/>
<attribute name="scope" type="spmlsearch:ScopeType" use="optional"/>
</extension>
</complexContent>
</complexType>
This would result in the following error when attempting to validate the xsds:
cos-nonambig: WC[##other:"urn:oasis:names:tc:SPML:2:0"] and "urn:oasis:names:tc:SPML:
2:0:search":basePsoID (or elements from their substitution group) violate "Unique Particle
Attribution". During validation against this schema, ambiguity would be created for those two
particles.
Since the XSDs are invalid themselves, i'm having a duzy of a time figuring out how i can actually validate payload requests against the XSDS provided by the spec itself.
Adding the PayloadValidatingInterceptor in the spring context file results in the same error when the server attempts to start:
<bean id="validatingInterceptor"
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="schemas">
<list>
<value>/WEB-INF/xsd/spml/pstc_spmlv2_core.xsd</value>
<value>/WEB-INF/xsd/spml/pstc_spmlv2_search.xsd</value>
</list>
</property>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="false"/>
</bean>
Thanks for anyone's input ahead of time, not sure if anyone ever ran into this type of issue or not.
Damian
What you need to do is disable the Unique Particle Attribution check. In general, this works in Java (see this SO post), you just need to find a way to configure it... Worst case might be to build your own validator based on the above link - it should work since Spring's XSD schemas are using Apache's implementation.

Resources